對於我的程序,如果用戶沒有輸入命令行參數並顯示錯誤消息,那麼我在創建錯誤檢查時遇到問題。而不是崩潰。 沒有文本文件參數,我得到一個ArrayIndexOutOfBoundsException:o?錯誤檢查命令行參數
public static void main(String[] commandlineArgument) {
Integer[] array = ReadFile7.readFileReturnIntegers(commandlineArgument[0]);
ReadFile7.printArrayAndIntegerCount(array, commandlineArgument[0]);
if(commandlineArgument.length == 0){
System.out.println("Please file name " +
"as the first commandline argument.");
}
}
只需推動 '如果(commandlineArgument.length == 0){...' 塊到主要方法的頂部。並添加'返回1;'裏面如果阻止執行 –