所以我创建了一个InvalidInputExeption
异常类,并且如果没有有效的输入,则会在setter方法中使用它来抛出异常。
public class kalsi.ContestantInformation {
public void setFirstName(String firstName) throws InvalidInputExeption {
checkInput(firstName, "Please only enter letter in the first name");
this.firstName = firstName.replaceAll("\\s", "").toLowerCase();
}
public void setLastName(String lastName) throws InvalidInputExeption {
checkInput(lastName, "Please only enter letter in the last name");
this.lastName = lastName.replaceAll("\\s", "").toLowerCase();
}
public void setStreetNumber(String streetNumber) throws InvalidInputExeption {
checkInput(streetNumber, "Please only enter numbers in the street number");
this.streetNumber = streetNumber.replaceAll("\\s", "").toLowerCase();
}
public void setStreetName(String streetName) throws InvalidInputExeption {
checkInput(streetName, "Please only enter letter in the street name name");
this.streetName = streetName.replaceAll("\\s", "").toLowerCase();
}
public void checkInput(String s, String message) throws InvalidInputExeption {
char[] array = s.toCharArray();
for (char c : array) {
if (!Character.isLetter(c)) {
throw new InvalidInputExeption(message);
}
}
}
public void checkInput(int i, String message) throws InvalidInputExeption {
String s = i + "";
char[] array = s.toCharArray();
for (char c : array) {
if (!Character.isDigit(c)) {
throw new InvalidInputExeption(message);
}
}
}
}
这是我使用反射调用setFirstName
的主要方法。
注意setFirstName
不是唯一的setter方法。所有其他方法都在一个字符串数组中,其名称为setMethods
。
public static void main(String[] args)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
SecurityException, ClassNotFoundException, InstantiationException {
ContestantInformation contestant1 = new ContestantInformation();
String[] questions = { "What is your first name", "What is your last name", "What is your Street Name",
"What is your Sreet Number" };
String[] methods = { "setFirstName", "setLastName", "setStreetName", "setStreetNumber" };
for (int i = 0; i < methods.length; i++) {
do {
try {
flag = false;
System.out.println(questions[i]);
String scannerInput = scanner.nextLine();
classContestantInfo.getDeclaredMethod(methods[i], stringParameter).invoke(contestant1,
scannerInput);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof InvalidInputExeption) {
System.out.println(e.getMessage());
}
}
} while (flag);
}
}
问题是,当我输入一些不是String的东西时,它不会再问这个问题,或者在这里输出错误信息就是输出。
What is your first name 9 null What is your last name
它输出null
并转移到下一个问题,即使它应该输出&#34;请只输入名字中的字母&#34;然后再次询问用户输入。
这是git clone URL,如果有人想要克隆代码,那么程序就是ICS4U存储库中的RealityShowApplication:
https://github.com/simar1998/ICS4U.git
答案 0 :(得分:1)
您正在显示InvocationTargetException
的消息,而不是InvalidInputException
的消息。
您还希望通过将repeat-flag设置为true
来确保重复该问题。
将catch子句主体更改为:
if (e.getCause() instanceof InvalidInputExeption) {
System.out.println(e.getCause().getMessage()); // <-- note: getCause() here
flag = true; // Make sure that the question is repeated
}
我建议将flag
更改为inputError
,repeatFlag
或其他更具描述性标记功能的内容。
答案 1 :(得分:1)
除了此处发布的其他答案之外,在您成功调用之前,您没有继续循环并询问相同问题的原因似乎是您设置了flag = false
将检查输入的方法。此外,您应该在flag = true
循环的开头重置for
,以便如果第二个或后续问题的输入无效,您将继续循环该问题直到它被回答。