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
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)
What I want to do :
I made a BMI calculator (Formula=(KG*KG)/M)
The input takes eg 186 but i get an error when inserting 1.86 (For height in metres)
java.lang.NumberFormatException: For input string: "1.86"
Here is my code :
if (txtHeight.getText().length() > 0 && txtWeight.getText().length() > 0){
height = Integer.parseInt(txtHeight.getText());
heightsqr = (int) Math.pow(height, 2);
mass = Integer.parseInt(txtWeight.getText());
bmi = (heightsqr/ mass);
lblBmi.setText("Current BMI : " + Integer.toString(bmi));
}
else{
JOptionPane.showMessageDialog(this, "Please enter your weight in KG and your Height in M");
}
It's a pretty basic error that I just cant seem to fix
As the stacktrace says, you can't convert 1.86 to int. Why? because it's a double.
try:
mass = Double.parseDouble(txtWeight.getText());
In Java, we have types. int can only store integer. Real numbers can be stored in floats and doubles but remember, that you can lose precision.
I don't know your whole code, so I can't tell you, what is the type of mass. In case you have the following somewhere in your code:
int mass;
change it to:
double mass;
Some random thoughts:
As you have already seen, there is a possibility that the Exception will be thrown. It happens depending on user input I believe. In Java we have a mechanism to secure. It's a try-catch block. It can be used for example in the following way:
try {
mass = Double.parseDouble("1,86");
} catch (NumberFormatException e) {
//somehow notify user that he has mistaken and work it out according to your business logic.
}
I strongly recommend reading this tutorial which I have provided on StackOverflow question What is a NumberFormatException and how can I fix it.
an Integer is a non-decimal value, hence 1.86 is invalid.
Try converting it into a Double:
Double.parseDouble(txtHeight.getText());
you try to convert a double into an int. thats causing the error. if you want to fix it, you should either only accept integers or only doubles. otherwise you will need 2 if-statements. converting to double looks like this: height = Double.parseDouble(txtHeight.getText());
Sorry for this basic question, I have tried to check other answers on SO but they haven't been convincing. My code is plain and simple but I do not understand why do I have compiling errors.
Can someone explain this in layman terms? It would be very helpful.
public class gravityCalculator
public static void main(String[] args) {
// TODO Auto-generated method stub
double g= -9.81;
double u= 0.0;
double t= 10.0;
double x_i =0;
double s;
double a=0;
if(a<=0){
a=g;
}
//System.out.println(a);
s= 0.5+a.t^2+u.t+x_i;
}
}
Error Trace
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The primitive type double of a does not have a field t
The primitive type double of u does not have a field t
Edit: This question can also be useful to know about the difference between Datatype and an Object.
Solution : s= 0.5*a*t * t+u*t+x_i;
The multiplication symbol in Java is *, not .. Also, Java does not have an exponentiation operator (and ^ means something different than what you want). Correct syntax for what you're trying to do:
s = 0.5 + a*t*t + u*t + x_i;
Here I just multiplied t*t to get t2. In general, exponentiation can be calculated with the Math.pow() method.
EDIT: Assuming that you want to calculate position at a given time t for a given start position x_i, velocity v and acceleration a, you not only need to correct the syntax as above, but you also need to correct the formula:
s = 0.5*a*t*t + u*t + x_i;
(The 0.5 needs to be a coefficient of at2, not an added term.)
In your expression s= 0.5+a.t^2+u.t+x_i; there is a part u.t that confused the compiler.
It supposed that u had a public property (field) with name t, but it did not. So you get the error.
u has primitive type double. It is not an object, so it does not have any properties.
Rewrite your expression like that: s = 0.5 + a*t*t + u*t + x_i;
It's the line s= 0.5 + a.t
what were you trying to achieve there? perhaps multiplication? in that case it's asterisk:
0.5 + a * t
s= 0.5+a.t^2+u.t+x_i;
You are writing . instead of * for multiplication. . is used to denote methods of fields of the object and primitive objects have no methods or attributes.
Edit: ^ is used as XOR. To square, you have to multiply t*t or use java.lang.Math.pow (I would not recommend using this one as it is way slower.).
The problem is in this part t^2 in java to make a power use Math.pow(), to get t^2 use Math.pow(t,2), also, to multiply in java you have to use * operator instead of . that means object property or attribute, so your complete sentence must be:
Also, to make a correct calculation you must think in operations order, mutiply and divide goes first, so you have to be carefull with parenthesis to get correct result!
s= (0.5+a)*Math.pow(t,2)+u*(t+x_i);
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);
can anybody look at this code and tell me why the exception happens?
public static void main(String[] args)
{
int total =100;
int discount_Ammount = 20 ;
int newAccount=Integer.parseInt( String.valueOf(Math.floor(total - discount_Ammount)).trim());
}
Method floor returns double value , then I make casting to integer, so I cast it to string then to integer... please, can anybody help?
You aren't "casting" anything. trim() removes whitespace only, which will never be present in the result of String.valueOf(double).
Use a cast:
int newAccount = (int) Math.floor(total - discount_Ammount);
Java is a strongly typed programming language, not a scripting language. Implicit conversions between strings and other types are not supported.
Or, get rid of the floor() operation altogether, since you are working with an int quantity already, and floor() is meaningless:
int newAccount = total - discount_Ammount;
If you are working with money, use the BigDecimal class so that you can use the round-off rules required by your accounting system. You won't have control of that when using double.
Did you try this?
int newAccount = (int) Math.floor(total - discount_Ammount);
Or even this!
int newAccount = total - discount_Ammount;
No need to do Integer.parseInt( String.valueOf(
To cast to int, just do (int)(blah)
So int newAccount=(int)(Math.floor(total - discount_Ammount));