這就是我想要做的:如何在java eclipse中捕獲輸入鍵作爲錯誤?
用戶輸入數字到程序中,程序做了什麼。
,但如果用戶只需鍵入「回車」鍵(或「返回」鍵)日食說
異常線程「main」 java.lang.StringIndexOutOfBoundsException:字符串索引超出範圍: 0
我需要爲eclipse說的是「無效輸入」並再次請求輸入。
我這是怎麼獲取輸入:
我在一個函數中使用一個緩衝的讀者,我調用該函數每當我需要讀取用戶的輸入。這是我的這個函數的代碼,因爲你想引用它。此外,該計劃是一個覆蓋優化計劃,因爲你也想知道這一點。非常感謝你的幫助!
public static String Reader() {
BufferedReader nerd = new BufferedReader(new InputStreamReader(System.in));
String rvalue = "0";
try {
rvalue = nerd.readLine();
if(elements_typed == 0) {
Integer.parseInt(rvalue);
}
if(sets_typed == 0) {
Integer.parseInt(rvalue);
}
return rvalue;
} catch (IOException e) {
System.out.print("ERROR: Input must be an integer in [1, infinity]!");
display_menu();
} catch (NumberFormatException e) {
System.out.print("ERROR: Input must be an integer in [1, infinity]!\n\n");
if(elements_typed == 0) {
System.out.print("Enter number of elements (n):\n");
rvalue = Reader();
}
if(sets_typed == 0) {
System.out.print("Enter number of sets (n):\n");
rvalue = Reader();
}
}
return rvalue;
}
請抽出時間來正確地格式化代碼。 –
什麼是完整的堆棧跟蹤?它說什麼行號引發異常? –
按下回車就會輸入一個換行符 –