This question already has answers here:
Why does the division of two integers return 0.0 in Java? [duplicate]
(6 answers)
Integer division: How do you produce a double?
(11 answers)
Closed 6 years ago.
In this block of code which is inside a broadcast receiver I have a calculation.
My goal is to get a battery bar with the correct amount of Green inside it. So for the Green I have an image which is essentially a block of solid green. I am not sure if this is the best way to do this but I decided to resize the block using this calculation:
(Height of Block) * (Battery Level/100)
The actual code:
BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context ctxt, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 100);
percentageView.setText(String.valueOf(level)+"%");
int height_val = (363) * (level / 100); //<-- That's the int which always returns 0
Log.e("MyTag", String.valueOf(height_val).toString());
Log.e("MyTAgg", String.valueOf(363 * (level/100)));
bar.requestLayout();
bar.getLayoutParams().height= height_val;
}
};
And it is inside a class. It's registered in the class not in the manifest. My Text View shows me the battery percentage but my Tags always show me that my calculations returns 0. Please do help me understand. Why does this calculation return 0?
Thank you.
Related
This question already has answers here:
Why is Java's division broken?
(6 answers)
Closed 3 years ago.
This works: Thread.Sleep(1000)
This does not work: Thread.Sleep(frequency)
The frequency variable takes edittext input from a user and parses it into an integer so it should pass just fine. I have no idea what I'm doing wrong. Help appreciated :)
private void FlashButtonClicked() {
startPattern = true;
try{
frequency = Long.parseLong(frequencyInput.getText().toString());
}catch(NumberFormatException e){
Toast.makeText(getBaseContext(), "value not acceptable", Toast.LENGTH_LONG).show();
}
frequency = (1 / frequency) * 1000;
while(startPattern){
enableTorch();
try{
// see if you can figure out why I can't pass the user input into the
//Thread.sleep() method without it fucking up. When you change it manually with
// a long input, it works just fine.
Thread.sleep(frequency);
}catch(Exception e){
e.printStackTrace();
}
}
}
If frequency is of type long and is greater than 1, then 1/frequency is zero (because / means integer division). Replace it with: frequency = 1000/frequency
This question already has answers here:
Why do these two multiplication operations give different results?
(2 answers)
Closed 6 years ago.
I am confused how it's happening. Output is not as expected.
public class Test2 {
public static void main(String arg[]){
int interval = 43200;
long tempInterval = interval * 60000;
System.out.println(tempInterval);
}}
Expected output is 2592000000 but I'm getting -1702967296. It might be naive
question but I'm stuck with this.
Add a L after 60000.
Java assumes that you're using int multiplication, which causes an int overflow before it's casted to a long.
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 6 years ago.
I have recently begun programming at university and I am a little stumped with one of my tutorial problems.
I basically need to create a method within a class which uses the Random.nextInt()
method to flip a coin, assigning and saving the value once the process has been run.
MY current attempts include this:
public void Flip() {
int flipResult;
flipResult = mRandNumGen.nextInt(1);
if(flipResult == 0)
{
mFace = 'H';
}
else
{
mFace = 'T'
}
}
mFace and mRandNumGen are variables which have been declared already outside the specific method. What exactly is going wrong here? I can't for the life of me get this to work :/
Simple way to do this:
if (mRandNumGen.nextBoolean()) {
mFace = 'H';
} else {
mFace = 'T';
}
The first argument in Random.nextInt is an exclusive upper bound, not inclusive.
So with n=1 it will always return 0. for n=2 it will return either 0 or 1 which is what you're looking for.
This question already has answers here:
android.content.res.Resources$NotFoundException: String resource ID #0x0
(8 answers)
Closed 6 years ago.
I am following a beginner's android development course on Udacity and have seemed to come across a simple error, though I cannot for the life of me figure it out or how to search for it.
There is just a number with an increment and decrement button on the sides to change it. The problem comes either when I call the displayQuantity method or when it tries to set the text of the quantityTextView. The value of quantity does change, but the app closes before it changes on the screen.
public void increment (View view){
quantity = quantity + 1;
displayQuantity(quantity);
}
public void decrement (View view){
quantity = quantity - 1;
displayQuantity(quantity);
}
private void displayQuantity(int quantity) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText(quantity);
}
Change to
quantityTextView.setText(Integer.toString(quantity));
TextView.setText() is overloaded. The version that takes an integer expects a resource identifier for a string resource (e.g. R.string.something), but the value you are passing doesn't correspond to any such resource.
This question already has answers here:
Collision Detection with MANY objects
(3 answers)
Closed 9 years ago.
I have two moving image views in a little game I'm making,
anyway they are always in the same Y so im just want to check when they are in the same x
any help?
int [] object1Position = new int[2]; // declare int array for x,y object position
int [] object2Position = new int[2]; // the same
imageView1.getLocationOnScreen(object1Position); // get imageView1 position
imageView2.getLocationOnScreen(object2Position); // get imageView2 position
public boolean checkColisionOnXAxis()
{
if (object1Position[0] == object2Position[0] )
return true;
return false;
}
just an idea.. but you need to get at each time the current location of the objects..
so you need timer or something like that to get the current position