线程“main”中的异常java.lang.NumberFormatException:对于输入字符串:“S”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18682742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception in thread "main" java.lang.NumberFormatException: For input string: "S"
提问by Mattias S
I get this error when I try to use Integer.parseInt() with a single char.
当我尝试使用带有单个字符的 Integer.parseInt() 时出现此错误。
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"
采纳答案by Joni
The letter S
is not a number. Did you mean to write the number 5?
字母S
不是数字。你是想写数字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
?
或者你的意思是打印字符的 ASCII 或 Unicode 值S
?
char s = 's';
System.out.println((int) s);
回答by darxtrix
yes of course.. Integer.parseInt can make the integer representation of only numeric strings.Try:
是的当然.. Integer.parseInt 可以使仅数字字符串的整数表示。尝试:
String s="98"
字符串 s="98"
回答by Oleg Pyzhcov
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.
parseInt(String s)
用于将字符串形式的整数转换"42"
为它们以十进制表示的值。String.charAt(0)
如果您想要第一个字符,请使用。
回答by mawia
To parse an string to a number you must have valid number into the string.
要将字符串解析为数字,您必须在字符串中包含有效数字。
Here your S is not a number.
这里你的 S 不是数字。
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));
回答by Gyanendra Dwivedi
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.
如果不是,Integer.parseiInt()
则必然会失败。
I would recomend you to go for really quick and short tutorial located at http://www.tutorialspoint.com/java/number_parseint.htm
我会建议你去位于http://www.tutorialspoint.com/java/number_parseint.htm 的非常快速和简短的教程