我放弃了。我试图弄清楚为什么我的代码返回第一个(不正确的)值。
例如:
public static String lastName() {
System.out.println("Last Name: ");
String lastName = input.nextLine();
String dataCheck = "[a-zA-Z]{1,77}";
Pattern pattern = Pattern.compile(dataCheck);
Matcher patternMatcher = pattern.matcher(lastName);
if(!patternMatcher.matches()){
do{
System.out.println("Incorrect input:\n"
+ "1) Shorten the first name less than 77 characters;\n"
+ "2) Only alphabet characters are allowed (no numbers or special characters.)\n\n"
+ "Please enter Last Name: ");
lastName();
}
while (!patternMatcher.matches());
}
return lastName;
}
public static void recall(String lastName){
System.out.println(lastName);
}
如果我输入了错误的输入,扫描仪"存储"会发生什么?第一个输入,只使用它;如果输入正确,则继续。到目前为止的研究告诉我要废弃正则表达式并使用input.hasNextLine()。任何帮助将不胜感激。