0

I want to make a while loop so that whenever a user inputs a blank input, it will re-ask the question until it is not empty. So far, I have this:

        Scanner scn = new Scanner(System.in);

        while (//user input is not blank) {

            System.out.print("Enter id: ");
            int id = scn.nextInt();

            System.out.print("Enter name: ");
            String last_name = scn.next();

            System.out.print("Enter phone: ");
            String first_name = scn.next();

            scn.close();
            break;
        }

i'm pretty sure i'm over thinking this but i'm not sure of the syntax or the functions.

Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399
evelyn
  • 89
  • 6
  • You should also read about exceptions. The Scanner will throw exceptions on input being incorrect (like when the user enters something that can't be parsed into an integer). – Nathaniel Johnson Jan 09 '16 at 17:35

1 Answers1

1
  1. You should expect user to type say Quit/quit to quit rather than empty string.
  2. You should close scanner out of loop without break.
  3. You should use do...while loop instead if while loop. Something like:

    do {
       ...
       exit = scn.next();
    } while (!exit.equalsIgnoreCase("quit"));
    scn.close();
    
SMA
  • 33,915
  • 6
  • 43
  • 65