英文:
Java next() and nextLine errors
问题
我正在尝试在Java中使用ArrayList实现电话簿,但无论何时我尝试使用next()或nextLine()来获取用户信息,都会出现错误。这是收集和保存用户输入的方法。
public void saveContact() {
String[] userInput = new String[3];
String[] input = {"name", "age", "phone number"};
String[] pattern = {".", "\\d", ".+\\d+"};
for (int i = 0; i < 3; i++) {
try (Scanner sc = new Scanner(System.in)) {
String name = "";
while (!verifyUserInput(name, pattern[i])) {
System.out.printf("Enter contact's %s\n", input[i]);
name = sc.nextLine();
userInput[i] = name;
}
}
}
phoneArrayList.add(userInput);
}
当我使用nextLine()时,会出现以下错误:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at PhoneDirectory.saveContact(PhoneDirectory.java:42)
at MainClass.main(MainClass.java:19)
而当我使用next()时,会出现以下错误:
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at PhoneDirectory.saveContact(PhoneDirectory.java:42)
at MainClass.main(MainClass.java:19)
我尝试过像这样使用hasNext()和hasNextLine(),但会导致无限循环。我不知道我应该使用什么来获取用户输入而不会出现错误。
if (sc.hasNext()) {
name = sc.next();
}
这是verifyUserInput方法:
public boolean verifyUserInput(String input, String patt) {
Pattern pattern = Pattern.compile(patt);
Matcher m = pattern.matcher(input);
return m.find();
}
英文:
I'm trying to implement a phonebook in Java with ArrayLists but whenever I try to get user info using either next() or nextLine() I get errors. This is the method that collects and saves the user input.
public void saveContact() {
String[] userInput = new String[3];
String[] input = {"name", "age", "phone number"};
String[] pattern = {".", "\\d", ".+\\d+"};
for(int i=0; i < 3; i++) {
try (Scanner sc = new Scanner(System.in)) {
String name = "";
while(!verifyUserInput(name, pattern[i])) {
System.out.printf("Enter contact's %s\n", input[i]);
name = sc.nextLine();
userInput[i] = name;
}
}
}
phoneArrayList.add(userInput);
}
Whenever I use nextLine() I get
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at PhoneDirectory.saveContact(PhoneDirectory.java:42)
at MainClass.main(MainClass.java:19)
And when I use next() I get
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at PhoneDirectory.saveContact(PhoneDirectory.java:42)
at MainClass.main(MainClass.java:19)
I've tried using hasNext() and hasNextLine() like this but it just results in an infinite loop. I have no idea what I'm supposed to use to get user input without having errors.
if(sc.hasNext()) {
name = sc.next();
}
And this is the verifyUserInput method
public boolean verifyUserInput(String input, String patt) {
Pattern pattern = Pattern.compile(patt);
Matcher m = pattern.matcher(input);
return m.find();
}
答案1
得分: -2
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String[] userInput = new String[3];
String[] input = {"name", "age", "phone number"};
String[] pattern = {".", "\\d", ".+\\d+"};
boolean isAllInputValid = true;
for (int i = 0; i < 3; i++) {
System.out.printf("Enter contact's %s\n", input[i]);
if (scan.hasNextLine()) {
String inputStr = scan.nextLine();
if (!verifyUserInput(inputStr, pattern[i])) {
isAllInputValid = false;
break;
}
userInput[i] = inputStr;
System.out.println("contact's " + input[i] + " is " + inputStr);
}
}
if (isAllInputValid) {
System.out.println("userInput = " + Arrays.toString(userInput));
}
}
public static boolean verifyUserInput(String input, String patt) {
Pattern pattern = Pattern.compile(patt);
Matcher m = pattern.matcher(input);
return m.find();
}
}
英文:
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String[] userInput = new String[3];
String[] input = {"name", "age", "phone number"};
String[] pattern = {".", "\\d", ".+\\d+"};
boolean isAllInputValid = true;
for (int i = 0; i < 3; i++) {
System.out.printf("Enter contact's %s\n", input[i]);
if (scan.hasNextLine()) {
String inputStr = scan.nextLine();
if (!verifyUserInput(inputStr, pattern[i])) {
isAllInputValid = false;
break;
}
userInput[i] = inputStr;
System.out.println("contact's " + input[i] + " is " + inputStr);
}
}
if (isAllInputValid) {
System.out.println("userInput = " + Arrays.toString(userInput));
}
}
public static boolean verifyUserInput(String input, String patt) {
Pattern pattern = Pattern.compile(patt);
Matcher m = pattern.matcher(input);
return m.find();
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论