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"
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).
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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With