like “1+23”
parse to double for example.. then calculate..
but when i have a decimal the program crashes for example “1.1+2” the program glitches on the 1.1 when i’m parsing it
if(s.contains("+"))
{
int n = s.indexOf("+");
String w1 = s.substring(0,n);
String w2 = s.substring(n+1,s.length());
part1= (long) Double.parseDouble(w1);
part2 = (long)Double.parseDouble(w2);
Exception in thread “AWT-EventQueue-0” java.lang.NumberFormatException: For input string: “1.1”
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
why are you casting from a double to long? that will result in your number being truncated. in the case of 1.1 you will get 1. What you should do is use the following:
where w1 is your floating point value.
you cannot have part1 be a long value, since long values cannot hold floating points. part1 must be a float.