0

Can any body help me out to solve this issue

String expr ="1.3*60";
//Integer xx=Integer.valueOf();
int i = Integer.parseInt(expr);
System.out.println("xx "+i);

When i am compiling this it shows Number Format Exception, I am in Beginning Stage in java, so please help me out to solve this, Thanks a ton

Chris Dargis
  • 5,891
  • 4
  • 39
  • 63
SANJAY
  • 9
  • 4
  • 8

5 Answers5

7

Integer.parseInt does not evaluate mathematical expressions; it simply converts a String format of an integer to an Integer type.

For example:

int x = 5;
int y = Integer.parseInt("5");

x == y; // => true

To evaluate mathematical expressions like 1.3 * 60 = ?, you might want to search for Java math parsing/evaluation libraries. (see this question, for example)

Community
  • 1
  • 1
Jon Newmuis
  • 25,722
  • 2
  • 45
  • 57
3
"1.3*60";

is not a valid integer.

Integer.parseInt(...); can parse only valid integer

If you want to use expression evaluation engines. See this and this

Community
  • 1
  • 1
kosa
  • 65,990
  • 13
  • 130
  • 167
0
String[] tokens = expr.split(delims);
...
int i = Integer.parseInt(expr);

You've already split the string into a float and an int, so you need to pass your "tokens[0]" and "tokens[1]" strings into parseInt() instead of your original string "expr", although on of those strings is a float, so I'm not sure if even that will work.

Aaron Ballard
  • 581
  • 6
  • 10
0

Check this code:

String expr ="1.3*60";
String[] tokens = expr.split("\\*");
float j = Float.parseFloat(tokens[0]);
float k = Float.parseFloat(tokens[1]);
System.out.println(j);
System.out.println(k);
System.out.println(j*k);

So, first you have to tokenize using a regular expresion (I wont talk about that), so that is the split part you had. Then, as some of the numbers you are parsing are not integers you should use floats or the corresponding type, or use try/catchs or whatever to ignore non int values. And lastly, valueOf returns objects while parseX returns primitives, which I think are the ones you need.

pamo
  • 81
  • 1
  • 5
  • Yes i agree, that expression will be dynamically loaded from the DB,it may be expr=(1.3+60-80), so any solution for dynamic expressions – SANJAY Oct 30 '12 at 04:07
  • Then, you will have to add more logic while splitting your tokens, considering all the possible operators and their precedences. So your code may be as complex as the dynamic expressions to be parsed. You could split by *, /, + and - and then iterate over the generated arrays. The other way around, using the js engine is easier, but with a higher resources cost. – pamo Oct 30 '12 at 04:26
0

If you are using jdk 1.6, you can use inbuilt javascript engine to evaluate expression:

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;

public class expressionEvaluate{
  public static void main(String[] args) throws Exception{
    ScriptEngineManager sem = new ScriptEngineManager();
    ScriptEngine scriptEngine = sem.getEngineByName("JavaScript");
    String expr = "1*3+5";
    System.out.println(scriptEngine.eval(expr));
    } 
}
Kishor Sharma
  • 599
  • 8
  • 15