我试图将从键盘获取的字符串值转换为int值。我已经做了这样的之前,但现在我得到一个错误,指出NumberFormatException.forInputString
从扫描器输入到int转换的字符串
Scanner input = new Scanner(System.in);
String choice = "";
int numberChoice;
System.out.println("Please select one of the following options");
choice = input.nextLine();
numberChoice = Integer.parseInt(choice); /*I am getting the error on this line*/
输入的代码是:
Data[] temperatures = new Data[7];
for(int i = 0; i < temperatures.length; i++)
{
System.out.println("Please enter the temperature for day " + (i+1));
temperatures[i] = new Data(input.nextDouble());
}
http://stackoverflow.com/questions/18711896/how-to-resolve-java-lang-numberformatexception- for-input-string-na – generalcrispy 2014-10-10 17:29:10
您可能输入的内容不是有效的数字。如果这不是问题,那么还有其他问题导致我们看不到的代码出现问题。程序是否给你一个例外,但你没有机会输入任何内容?上述代码之前是否还有其他输入? – ajb 2014-10-10 17:38:39
我输入有效的int变量。是的,有输入的东西,我已经把它添加到原来的问题 – destroted 2014-10-10 17:41:20