Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception in thread "main" java.lang.NumberFormatException: For input string: "S" [closed]

Tags:

java

integer

I get this error when I try to use Integer.parseInt() with a single char.

String s = "s";
System.out.println((char) Integer.parseInt(s));

Is what gives me the error is this:

Exception in thread "main" java.lang.NumberFormatException: For input string: "S"
like image 963
Mattias S Avatar asked Sep 08 '13 10:09

Mattias S


People also ask

What is exception in thread main Java Lang NumberFormatException for input string?

The NumberFormatException is an unchecked exception in Java that occurs when an attempt is made to convert a string with an incorrect format to a numeric value. Therefore, this exception is thrown when it is not possible to convert a string to a numeric type (e.g. int, float).

How do I fix NumberFormatException?

NumberFormatException and how to solve them. It's one of those errors where you need to investigate more about data than code. You need to find the source of invalid data and correct it. In code, just make sure you catch the NumberFormatException whenever you convert a string to a number in Java.

Is NumberFormatException a runtime exception?

The NumberFormatException occurs when an attempt is made to convert a string with improper format into a numeric value. That means, when it is not possible to convert a string in any numeric type (float, int, etc), this exception is thrown. It is a Runtime Exception (Unchecked Exception) in Java.

Is null NumberFormatException?

NumberFormatException: For input string: "null" is specifically saying that the String you receive for parsing is not numeric and it's true, "null" is not numeric. Many Java methods which convert String to numeric type like Integer. parseInt() which convert String to int, Double.


2 Answers

parseInt(String s) is used to convert integers in string form like "42" to value they represent in decimal. Use String.charAt(0) if you want first character.

like image 73
Oleg Pyzhcov Avatar answered Oct 25 '22 22:10

Oleg Pyzhcov


The letter S is not a number. Did you mean to write the number 5?

String s = "5";
System.out.println((char) Integer.parseInt(s));

Or did you mean to print the ASCII or Unicode value of the character S?

char s = 's';
System.out.println((int) s);
like image 29
Joni Avatar answered Oct 25 '22 22:10

Joni