-6
public static void main(String[] args)throws IOException {
  String s ="12312a";
  int x = Integer.parseInt(s);
  System.out.println (x+2);
}

and all what I've got is :

Exception in thread "main" java.lang.NumberFormatException: For input string: "12312a"

any hints ?

ThisaruG
  • 3,222
  • 7
  • 38
  • 60

3 Answers3

5

Perhaps you meant

int x = Integer.parseInt("12312a", 16);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
2

You can't parse a String to an int if the String isn't a number.

eg:

This will compile

String num = "3245";
int x = Integer.parseInt(num);

This will not:

String s ="12312a";
int x = Integer.parseInt(s);

Remove the a from your String.

If you want to parse it to the hexadecimal value, use

int x = int x = Integer.parseInt(s, 16);

This will parse it to base 16 number.

ThisaruG
  • 3,222
  • 7
  • 38
  • 60
2

If you try to parse a String which is not a number you get a java.lang.NumberFormatException.

Maybe you want to parse a hexadecimal value, then you could use:

public static void main(String[] args) throws IOException {
 String s ="12312a";
 int x = Integer.parseInt(s,16);
 System.out.println (x+2);
}

I hope it helps. Have a nice day.

Leonid Glanz
  • 1,261
  • 2
  • 16
  • 36