0

I want convert a string to an integer but it doesn't work. I think the problem is that the string isn't "clean", see this example:

public class Test{
    public static void main(String[] args){
        String str = "(2+2)";
        int conv = Integer.parseInt(str);
        System.out.println(conv);
    }
}

Why doesn't it work? I could also define int x = (2+2); without any problems.

What exactly is the problem here and is there an easy way to solve it?


*Purpose: I have just finished a code that will detect if a math expression is correct (brackets, signs, .. arithmetic stuff). As example the string input is ((8+7)*2) and the program will return true. But now I need to find a way to calculate this and return the solution of it, 30. (If you want I can post my code too but I didn't want make this question seemingly long.)

Jason
  • 11,744
  • 3
  • 42
  • 46
cnmesr
  • 345
  • 3
  • 11
  • Java has no built-in code that can do that. – SLaks May 10 '17 at 21:42
  • `parseInt` meant to take a string which is a **number** and to convert it to this number. – SHG May 10 '17 at 21:42
  • Integer.parseInt() is expecting a number in string form so "5", "100" and so forth would work becuase they are integers. it cannot do non integer string values like "1.1", "(1)" and "(2+2)" and so forth – RAZ_Muh_Taz May 10 '17 at 21:44
  • You can use dedicated library, such as mXparser - please follow the tutorial http://mathparser.org/mxparser-tutorial/ – Leroy Kegan May 12 '17 at 10:02

2 Answers2

1

Why doesn't it work?

The String cannot be parsed to an integer due to some of the invalid characters within here:

"(2+2)"

You simply cannot parse invalid characters such as ( ) and + using Integer.parseInt. However, when + is the only character used and is a leading sign it's valid i.e

String strOne = "+2";
String strTwo = "+2345";
String strThree = "+237645";

etc...

is there an easy way to solve it?

There is no built-in method to do this for you. However, you can have a look at other posts relating to your question:

Community
  • 1
  • 1
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • If you simple remove `(`, `+`, and `)`, you get `"22"`, which will parse to number `22`, which is obviously wrong, since expression should evaluate to number `4`. – Andreas May 10 '17 at 21:47
  • @Andreas I wasn't referring to parsing _expressions_.... this is part of the question posted _What exactly is the problem here_ and I answered that. – Ousmane D. May 10 '17 at 21:48
  • The "problem here" is not that the string is an invalid integer *value*, it's that the `parseInt()` method is not an expression evaluator. --- Oh, and bad link there!! – Andreas May 10 '17 at 21:51
  • hence I said _You simply cannot parse invalid characters such as ( ) and +._ which means I am referring to the `parseInt()`. – Ousmane D. May 10 '17 at 21:52
  • You *can* "parse" that string, if you use an *expression parser*. Saying that the string cannot be parsed is wrong. If you had said that it cannot be parsed by `parseInt()`, you'd be correct, but you're not saying that. – Andreas May 10 '17 at 21:53
  • Alright, I will edit my answer to get your point across, Thank you!. – Ousmane D. May 10 '17 at 21:54
  • `parseInt()` can parse `+`, but only as a leading sign. Saying that `parseInt()` cannot parse `+` is wrong. – Andreas May 10 '17 at 21:56
  • Thanks once again, apologise for the wording, I am a bit off today. – Ousmane D. May 10 '17 at 21:57
1

Integer.parseInt expects a string, however it needs a number in a string format.

The (2+2) is a string, and it cannot compute 2+2. So trying to convert non numeric values won't work. (), and + are non numeric values

UPDATE:

From my research you can use built-in Javascript engine.

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

public class Main {
  public static void main(String[] args) throws ScriptException {
    String str = "2+2";
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    System.out.println(engine.eval(str));
    } 
}
Community
  • 1
  • 1
Shank
  • 1,387
  • 11
  • 32
  • Booo is there really no method in java? I'm not allowed to import stuff which is outside java eclipse library. Does that mean I have to code it myself, somehow? – cnmesr May 10 '17 at 21:47
  • @cnmesr If you read the answers given in the duplicate link, you'll find you can do it without adding any libraries, and without writing your own. – Andreas May 10 '17 at 21:49
  • 1
    @cnmesr see updated code – Shank May 10 '17 at 21:55
  • *"The `(2+2)` is a string, and it cannot compute `2+2`."* You're right, a "string" cannot compute anything. It's just a string. --- Also, `+` can be part of a numeric value, if used as a leading sign. – Andreas May 10 '17 at 21:59