java中的数据类型转换错误
获取意外错误java中的数据类型转换错误,java,type-conversion,Java,Type Conversion,获取意外错误 import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner input = new Scanner(System.in) ; int number = input.nextInt() ; String word = input.nextLine(); String
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner input = new Scanner(System.in) ;
int number = input.nextInt() ;
String word = input.nextLine();
String word1 = Integer.toString(number) ;
int number1 = Integer.parseInt(word) ;
if (number1 == Integer.parseInt(word) )
{
number1 = number + 10 ;
System.out.println("Congratualtion its a number"+" "+ number1) ;
}
else if (word1 == Integer.toString(number) )
{ word1 = 10 + word ;
System.out.println("Congratualtion its a word"+" "+ word1);
}
else
System.out.println("Something is wrong !!! ") ;
}
}
错误消息:
Error : Exception in thread "main" java.lang.NumberFormatException: For input string: " "
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:481)
at java.lang.Integer.parseInt(Integer.java:527)
at Solution.main(Solution.java:13)
这是有问题的一行:
intnumber1=Integer.parseInt(word);
word
是非数字字符串;代码正在尝试将其转换为整数,但无法执行此操作。您可能希望打印出从输入中获得的每件东西,并确保您拥有您认为拥有的东西
祝您好运。NumberFormatException:对于输入字符串:“您注意到了吗?”?尝试正确输入数据为什么会出现意外情况?你以为会有不同的错误吗?为什么会投反对票?