-1

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();
    }
Jonas
  • 121,568
  • 97
  • 310
  • 388
  • How are you executing this program? Are you passing the values via the command line or do you submit your code somewhere? – Ivar Jul 24 '20 at 10:35
  • 1
    Does this answer your question? [java.util.NoSuchElementException: No line found](https://stackoverflow.com/questions/7209110/java-util-nosuchelementexception-no-line-found) – Alexey R. Jul 24 '20 at 10:36

1 Answers1

-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();
    }
}

simon5678
  • 249
  • 1
  • 5