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

0

5 Answers 5

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)

Sign up to request clarification or add additional context in comments.

Comments

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

6 Comments

Then wat is the solution for this
What you want to get, results of expression?
Yes i need the results of my expressions, and my expression should be declared in String
|
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.

Comments

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.

2 Comments

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
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.
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));
    } 
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.