Everything is set up fine in a simple app I'm creating and I can work with strings no problem, having a bit of an headscratcher on how to calculate an output based on some user input variables, below is where the button is performing the calculation, all the variables are set up fine to my knowledge I can post the full code if you want.
I need the output of 'lbm'.
'weight' and 'bodyfat' are both EditText's converted to int's and (hopefully) be calculated to provide the answer then pass it to 'lbmResult' which is a textView.
case R.id.btnCalcCalories:
int weight = Integer.parseInt(weightInt);
int bodyfat = Integer.parseInt(bodyfatInt);
lbm = weight*(100 - bodyfat)/100;
lbmResult.setText(lbm);
break;
}
Though you not mentioned whether you declared lbm as int or double,i assume the first case and look below,your code will be..
case R.id.btnCalcCalories:
int weight = Integer.parseInt(weightEdittextValue.getText().toString());
int bodyfat = Integer.parseInt(bodyfatEdittextValue.getText().toString());
int lbm = weight*(100 - bodyfat)/100;
lbmResult.setText(String.valueOf(lbm));
break;
But i suggest you need to use double instead of int to get the right result just like as..
case R.id.btnCalcCalories:
double weight = Integer.parseInt(weightEdittextValue.getText().toString());
double bodyfat = Integer.parseInt(bodyfatEdittextValue.getText().toString());
double lbm = weight*(100 - bodyfat)/100;
lbmResult.setText(String.valueOf(lbm));
break;
The code doesn't show it but lbm is likely of type int. Therefore setText(int) overload is used which expects a resource id but lbm isn't one. Change it to:
lbmResult.setText(Integer.toString(lbm));
to use the setText(CharSequence) overload instead.
setText accepts a String, and you are passing it a float. To print it, you must first convert lbm to a string.
try lbmResult.setText(Float.toString(lbm));
String temp=Integer.toString(lbm);
lbmResult.setText(temp);
also it's always better to use double for your calculations and format it to 2 decimal places
lbmResult.setText(lbm);
=>
lbmResult.setText("" + lbm);
Related
I am writing a java code on android studio and I want to do an operation for making a discount percentage to a number that is taken from the edit text as a string and it should be a double or int to use arithmetic operations on it so help me please find the way to solve it.
I am assuming that you want to convert the string to int or double.
String number = editText.getText().toString();
just write,
int n = Integer.parseInt(number);
now you can use n variable as you want.
percentage values are always floating numbers. for that an integer is not going to work well..
try instead a float or a double...
and use a widget that you can set/get without a string convertion, in that way you will avoid pains in the neck with Locale ways to represent those numbers...(is 13.5% the same as 13,5%?) etc
something like:
String x = editText.getText().toString();
just write:
double n = Double.parseDouble(x);
here the doc:
https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#parseDouble(java.lang.String)
Hello, here's my issue : I keep having an error telling me that the types are incompatible even though my "R.id.total_akylux" is a Number(Decimal) in the XML file, and the result is given in decimal. I don't really understand why do i keep having this error. If someone could help me, it'd be really useful. Thank you
First of all: Mind QBrutes comment and re-think your concept.
You are trying to assign a double to an int, this is exactly what the error tells you. Now that int you are using isn't even your number but the ID of your resource. If you really want to store an int in your resources, follow this answer: https://stackoverflow.com/a/19297523/2694254
Regarding your error
Double can't be assigned to int without some manual casting.
If you are confused by the int/double casting stuff:
int numberInt = 1;
double numberDouble = 1.8;
//what you are trying to do:
numberInt = numberDouble;
//what you could do:
numberInt = (int) numberDouble; //numberInt is now 1
//with rounding:
numberInt = (int) Math.round(numberDouble); //numberInt is now 2
Also, you could store a float in xml instead of int: https://stackoverflow.com/a/20120240/2694254
You could also store the double as String, but that would require even more casting.
First of all, I'd like to thank you to take time to answer me even though i'm new to this language. I understand what you're saying to me so i started changing my code like this
double prix = 15.90;
double m2 = (longueur_akylux*largeur_akylux)/100;
double total = prix*m2* quantite;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.akylux);
}
#SuppressWarnings("unused")
public void calcul_akylux(View v){
TextView t = (TextView) findViewById(R.id.total_akylux);
t.setText(total);
}
So, if i create a Number(Decimal) in the XML , it won't wait for a double ? How can i do so this Number is a decimal ?
Thank you
I have declared the variable for the double I'm using:
z= 345.876;
Now I want to display the number of digits that come before the decimal point. I tried to do the following:
String string_form = new Double(z).toString().subString(0,string_form.indexOf('.'));
double t = Double.valueOf(string_form);
I get an error saying: 'The method subString(int, int) is undefined for the type String'
Then a quick fix shows to change it small case s as: substring. However the error then changes to the string, 'string_form' which says it's not initialized. Any ideas on what to do?
And also how would I modify that to find the number of digits that come after a number? I know in the part
.indexOf('.')
I'd replace the decimal point with a number but how would i change it so that it displays how many digits come AFTER the number, not before? thanks. and yes I have imported the decimalformat text lang.
You're trying to use string_form before you have actually created it.
If you break
String string_form = new Double(z).toString().substring(0,string_form.indexOf('.'));
double t = Double.valueOf(string_form);
into
String string_temp = new Double(z).toString();
String string_form = string_temp.substring(0,string_temp.indexOf('.'));
double t = Double.valueOf(string_form);
Then it should work.
To get the numbers after the decimal point just take the digits from period until the end of the number.
String string_temp = new Double(z).toString();
String string_form = string_temp.substring(string_temp.indexOf('.'), string_temp.length());
double t = Double.valueOf(string_form);
As others have pointed out though, there are many better ways than converting to string and checking for period and reconverting.
The number of decimal digits before the decimal point is given by
(int)Math.log10(z)+1
The number of decimal digits after it is imprecise and depends on how much precision you use when converting to decimal. Floating-point values don't have decimal places, they have binary places, and the two are incommensurable.
just convert the double to a string. find the index of . with indexOf. get the length of the string. subtract the index of . from the length and you should have the count.
String string_form = Double(z).toString();
int index = string_form.indexOf('.');
double d = Double.parse(string_form.substring(0, index+1));
If the numbers are small, you could try
int i = (int) z;
or
long l = Math.round(z);
You're using string_form in the expression string_form.indexOf('.'), but it's not initialized yet, because it's only set after the call to substring(). You need to store the value of toString() in a temporary variable so you can access it in the call to substring(). Something like this
String stringForm = new Double(z).toString();
stringForm = stringForm.substring(stringForm.indexOf('.'));
There are other and better ways to do this, however. Math.floor(z) or even just a cast (long)z will work.
Could do this, for examble:
Integer.parseInt(String.valueOf(possibleBirths).split(".")[0]);
You can do the next thing:
Double z = 345.876;
int a = t.intValue();
int counter = 0;
while(a != 0) {
counter++;
a = a / 10; }
System.out.println(counter);
I'm just wondering why everyone declares binary numbers like this:
int val1 = 0b1010;
int val2 = 0b10000001;
when this declaring a number like this float val1 = 0b1010; works as well.
I'm working on a project that deals with image pixel color values, so I need to manipulate the saturation level of each color value (by reading them off the image) into a float between 0.0 and 1.0. I'm assuming declaring each initial binary as an integer has something to do with ease of manipulation when masking and such, but I haven't seen any particular rule of thumb other than every example that I've seen initializing the variables as an int.
(For my use an example would be:
double val = 0b100000; //this is equivalent to 32.0
System.out.println(test1/255.0); //this gives out the ratio, namely 0.12549...
In this case I would skip the need to cast anything...)
Thank you for taking the time to clear this up.
When you declare float val1 = 0b1010;, that is the equivalent of declaring float val1 = (float)0b1010; The cast from int to float is implicit, but it is still performed.
I have a button and TextView.
In the TextView I'm showing a value of a float number, with two digits after the dot.
The button is taking by each press a 0.01 from the current float number (which is being shown at the TextView).
In order to do so I've used the next code -
float hightCurrent = Float.parseFloat(hightNum.getText().toString());
hightCurrent -= 0.01;
DecimalFormat myFormatter = new DecimalFormat("##0.00");
myFormatter.format(hightCurrent);
if(myFormatter.equals("1.10")){
Log.d("GOT", "Got in");
}else{
}
hightNum.setText((myFormatter.format(hightCurrent)));
NOw as you can see I have an if statement.
What I want to know is when the value is 1.10.
The thing is that when the value that being shown is 1.10 it sure don't get into the if statement.
So how can I make this code work? how can I know when the value is 1.10?
Thanks for any kind of help
Assign myFormatter.format(hightCurrent); to a String variable
Compare the value of this var
Hence;
String str = myFormatter.format(hightCurrent);
if(str.equals("1.10")){
...
String newCurrent=myFormatter.format(hightCurrent);
and check newCurrent.equals("1.10")
Try it.