1

I have the following Java code:

int i = 6;
int j = 2;
String[] operators = {"+", "-", "*", "\\"};
for(String operator : operators) {
    System.out.print(Integer.valueOf(i + operator + j));
}

But I get:

Exception in thread "main" java.lang.NumberFormatException: For input string: "6+2"

How to solve this? The result should be:

8
4
12
3

Thanks!

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
Hans Spigel
  • 167
  • 2
  • 8
  • What do you expect to be the integer value of `+`? – GBlodgett Dec 11 '18 at 21:04
  • @GBlodgett Thanks but if I change with `Integer.parseInt(operator)` I get `Exception in thread "main" java.lang.NumberFormatException: For input string: "+"`. – Hans Spigel Dec 11 '18 at 21:04
  • Integer.valueOf is the wrong method! It tries to read the number of the string, e.g. "Integer.valueOf("123"). – mrbela Dec 11 '18 at 21:06

0 Answers0