我試圖將從鍵盤獲取的字符串值轉換爲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