0

I am creating an "add customer" method and I want to validate the "add forename" scanner so that only a string can be entered and if the user enters an int etc it will throw up an error, below is the code I have got so far. I have used "Harry" as an example, the concept seems to be working but obviously not every customer is going to be called Harry. Any help would be welcome!

boolean validInput12 = false;
            String userOption13 = "";
            do {
                System.out.println("Enter Customer Forename");
                if (sc2.hasNext()) {    
                    userOption13 = sc2.next();
                    if (userOption13.matches("Harry")) {
                        validInput12 = true;
                    sc2.nextLine();
                } else {
                    System.out.println("Input invalid: Please select a valid customer forename");
                }
            } else {
                System.out.print("Input invalid: Please enter a valid name \n");
                sc2.nextLine();
            }
        } while (!validInput12);
TheLostMind
  • 35,966
  • 12
  • 68
  • 104

0 Answers0