Home > Net >  How to ask user for ONLY one word string input and produce error prompt in a try-catch block
How to ask user for ONLY one word string input and produce error prompt in a try-catch block

Time:10-03

I am trying to code a to-do list program. One function of this program is to search for the entries inside the string array. The user should only input a ONE WORD keyword so if the user inputs more than one word or none, a prompt should show telling the user to try again. The code I've written so far is inside a try-catch statement. Using next() scanner only takes the first word and disregards the rest when inputting a multiple-word keyword, instead of producing an Exception. Here is my code for it:

case 2:
                    int tries2 = 0;
                    String searchKeyword;
                    while(tries2 == 0) {
                        try {
                            System.out.println("Enter 1 keyword: ");
                            searchKeyword = sc.next();
                            tries2  ;
                            if(Quinones_Exer2.searchToDoList(searchKeyword, todoList)==-1) {
                                System.out.println("No item found with that keyword!");
                                System.out.println();
                            }
                            else {
                                System.out.println("Found one item!");
                                System.out.println("(" (Quinones_Exer2.searchToDoList(searchKeyword, todoList) 1) ")" " " todoList[Quinones_Exer2.searchToDoList(searchKeyword, todoList)]);
                                System.out.println();
                            }
                        }
                        catch(InputMismatchException e) {
                            System.out.println("Please input a proper single keyword!");
                            System.out.println();
                            sc.next();
                        }
                    }
                    break;
            }```

CodePudding user response:

From the docs of Scanner.next():

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.

You would need to call next() again to get the the rest of the input.

Much simpler would be to use Scanner.nextLine() to get entire line and then use String.split() with whatever delimiter you are using to get an array of all inputted keywords.

Edit: Scanner.next(pattern) may do what you are looking for. It throws InputMismatchException if the input does not match provided pattern(regex). Example:

scanner.next("^[a-zA-Z] $")

This requires the entire line to consist of lower and/or upper case letters and nothing else.

CodePudding user response:

Use Scanner.nextLine() then split the supplied string. If the length of array is greater than 1 or the supplied string is empty then issue an invalid entry message and have the User enter the string over again:

while(tries2 == 0) {
    searchKeyword  = "";
    while (searchKeyword.isEmpty()) {
        System.out.println("Enter 1 keyword: ");
        searchKeyword = sc.nextLine().trim();
        if (searchKeyword.isEmpty() || searchKeyword.split("\\s ").length > 1) {
            System.out.println("Invalid Entry! {"   searchKeyword 
                               "You must supply a single Key Word!");
            searchKeyword = "";
        }
    }
    tries2  ;
    // ... The rest of your code ...
}
  • Related