【发布时间】:2011-03-18 22:46:37
【问题描述】:
我正在使用 try-catch 异常处理编写这个程序:
Scanner keyboard = new Scanner(System.in);
String[] employees = new String[5];
boolean done1 = false;
//input and error exception for entering employee names into an array
for (int i = 0; i < 5; i++)
{//begin for
while (!done1)
{//begin while
System.out.println("Please enter employee's name: ");
try
{//begin try
employees[i] = keyboard.nextLine();
if (employees[i].length() == 0)
throw new Exception("No name was entered.");
if (employees[i].length() >= 31)
throw new Exception("Name entered contains too many "
+ "characters");
for (int check = 0; check < employees[i].length(); check++)
{//begin for
if(Character.isDigit(employees[i].charAt(check)))
throw new Exception("Input contains invalid "
+ "charaters.");
}//end for
done1 = true;
}//end try
catch (Exception a)
{//begin catch
System.out.println("Error: " + a.getMessage());
}//end catch
}//end while
}//end for
当我运行程序时,它会跳出 for 循环,并且只输入 i 的第一个实例,其余的为空。我怎样才能让程序留在这个循环中并让它保持错误检查?
【问题讨论】: