问题
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"
回答1:
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);
回答2:
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.
回答3:
yes of course.. Integer.parseInt can make the integer representation of only numeric strings.Try:
String s="98"
回答4:
To parse an string to a number you must have valid number into the string.
Here your S is not a number.
String s = "s"; System.out.println((char) Integer.parseInt(s));
It should be something like this:
String s = "100";//or whatever integer you want to parse.
System.out.println((char) Integer.parseInt(s));
回答5:
it is nothing to do with single or multichar value. A simple test of this is that, if you remove the ' (quotes) from the value, would you find a Integer?
If no, the Integer.parseiInt() is bound to fail.
I would recomend you to go for really quick and short tutorial located at http://www.tutorialspoint.com/java/number_parseint.htm
来源:https://stackoverflow.com/questions/18682742/exception-in-thread-main-java-lang-numberformatexception-for-input-string-s