因此,我创建了一个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
using反射的主要方法。注意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);
}
}
问题是,当我输入不是字符串的内容时,它不会再提出问题,也不会给出错误消息,这里是输出。
What is your first name 9 null What is your last name
它会输出null
并移至下一个问题,即使该问题应输出“请只输入名字中的字母”,然后再次要求用户输入。
如果有人要克隆代码,则以下是git clone URL:程序是ICS4U存储库中的RealityShowApplication:
https://github.com/simar1998/ICS4U.git
您正在显示的讯息,而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
或更能描述此标志功能的其他内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句