Integer.parseInt 没有将字符串解析为整数
Integer.parseInt not parsing a string into integer
Scanner sc = new Scanner(System.in);
length = sc.nextLine();
length_to_play = Integer.parseInt(length);
我曾尝试使用 length.trim() 和 length.replaceAll() 来丢弃空格,但没有奏效。
我在线程 "main" java.lang.NumberFormatException.
中遇到异常
Exception in thread "main" java.lang.NumberFormatException: For input string: "y"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at speedwords.first_activity(speedwords.java:27)
at speedwords.main(speedwords.java:338)
我认为您误解了
的功能
Integer.parseInt();
来自 Java 文档
Parses the string argument as a signed decimal integer. The
characters in the string must all be decimal digits, except that
the first character may be an ASCII minus sign <code>'-'</code>
(<code>'\u002D'</code>) to indicate a negative value. The resulting
integer value is returned, exactly as if the argument and the radix
10 were given as arguments to the
{@link #parseInt(java.lang.String, int)} method.
@param s a <code>String</code> containing the <code>int</code>
representation to be parsed
@return the integer value represented by the argument in decimal.
@exception NumberFormatException if the string does not contain a
parsable integer.
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}
当用户输入数字时,你应该使用sc.nextInt()
。这样你就不需要自己写解析了。
如果您需要字符串并希望将它们转换为 int
,您可以对输入字符串的每个字符分别使用 Character.getNumericValue(myChar)
。
你应该看看 Integer.parseInt(String s)
上的文档
如您所见,解析后的字符串被转换为整数值,如果该值不是整数,则会抛出 NumberFormatException
.
Why are you expecting it should return ASCII integer value?
Answer: For your kind information we are passing String, not a char
which has independent ASCII value. If you want the ASCII of each character in your String then you can have a look at this question
.
好的,我发布了我发现最适合我想要的案例的答案。不过谢谢大家的帮助。
Scanner sc = new Scanner(System.in);
length = sc.nextLine();
//length_to_play = Integer.valueOf(length); didn't work
char ch = length.charAt(0);
length_to_play=Character.getNumericValue(ch); //worked
Scanner sc = new Scanner(System.in);
length = sc.nextLine();
length_to_play = Integer.parseInt(length);
我曾尝试使用 length.trim() 和 length.replaceAll() 来丢弃空格,但没有奏效。 我在线程 "main" java.lang.NumberFormatException.
中遇到异常Exception in thread "main" java.lang.NumberFormatException: For input string: "y"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at speedwords.first_activity(speedwords.java:27)
at speedwords.main(speedwords.java:338)
我认为您误解了
的功能Integer.parseInt();
来自 Java 文档
Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign <code>'-'</code> (<code>'\u002D'</code>) to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the {@link #parseInt(java.lang.String, int)} method. @param s a <code>String</code> containing the <code>int</code> representation to be parsed @return the integer value represented by the argument in decimal. @exception NumberFormatException if the string does not contain a parsable integer.
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}
当用户输入数字时,你应该使用sc.nextInt()
。这样你就不需要自己写解析了。
如果您需要字符串并希望将它们转换为 int
,您可以对输入字符串的每个字符分别使用 Character.getNumericValue(myChar)
。
你应该看看 Integer.parseInt(String s)
如您所见,解析后的字符串被转换为整数值,如果该值不是整数,则会抛出 NumberFormatException
.
Why are you expecting it should return ASCII integer value?
Answer: For your kind information we are passing String, not a
char
which has independent ASCII value. If you want the ASCII of each character in your String then you can have a look at thisquestion
.
好的,我发布了我发现最适合我想要的案例的答案。不过谢谢大家的帮助。
Scanner sc = new Scanner(System.in);
length = sc.nextLine();
//length_to_play = Integer.valueOf(length); didn't work
char ch = length.charAt(0);
length_to_play=Character.getNumericValue(ch); //worked