How do I make a loop that commands the user to keep inputing a value until If conditions are met, with the aid of a try catch statement?

Zerenity :

Im trying to tell the user to keep inputing the right value format until its done properly. I want to display to the user that if he/she has typed in the wrong format (number of values), he/she should try again and the system will ask the user to input new values. How do I do that with the following code below?

And is the while statement at the bottom valid (properly written)? Like if the exception isnt triggered, stop "do:ing"

P.S, I know that my code below looks awful, as I'm a mere beginner and do not know how to format code properly

public class PersonTidbok {

   public static void main(String[] args){


      Scanner console = new Scanner (System.in);

      System.out.print ("Welcome to your interface, please select of the following: " +
                        "\nP, T, L, S, A, Q"); 

  choice = console.next().charAt(0);

      switch (choice){

 case P:

      do{ 

      System.out.print (" enter persnr in the format of (YYYYMMDD));

        try{

        persnr = console.nextInt();

           if ( persnr.length != 8);

             throw new FormatException();
      }

                catch (FormatException exception){
                System.out.println(exception + "You printed wrong format, try again")); 
   }
}
   while (!FormatException);
}
}
Arvind Kumar Avinash :

Do it as follows:

import java.util.Scanner;

public class PersonTidbok {
    public static void main(String[] argv) {
        Scanner console = new Scanner(System.in);
        boolean valid;
        char choice = '\0';
        String persnr;
        do {
            valid = true;
            System.out.print("Welcome to your interface, please select of the following (P, T, L, S, A, Q): ");
            String input = console.nextLine();
            if (input.length() != 1) {
                System.out.println("Invalid input. Try again.");
                valid = false;
            }
            choice = input.charAt(0);
        } while (!valid);

        switch (choice) {
        case 'P':
            do {
                valid = true;
                System.out.print("Enter persnr in the format of (YYYYMMDD): ");
                try {
                    persnr = console.nextLine();
                    if (!persnr.matches("[1-9]{1}[0-9]{7}")) {
                        throw new IllegalArgumentException("You printed wrong format, try again");
                    }
                    System.out.println("Processsing...");
                    // ...Processing of persnr should go here
                } catch (IllegalArgumentException e) {
                    System.out.println(e.getMessage());
                    valid = false;
                }
            } while (!valid);
            break;
        default:
            System.out.println("Wrong value for choice.");
        }
    }
}

A sample run:

Welcome to your interface, please select of the following (P, T, L, S, A, Q): a
Wrong value for choice.

Another sample run:

Welcome to your interface, please select of the following (P, T, L, S, A, Q): PT
Invalid input. Try again.
Welcome to your interface, please select of the following (P, T, L, S, A, Q): P
Enter persnr in the format of (YYYYMMDD): 01234567
You printed wrong format, try again
Enter persnr in the format of (YYYYMMDD): ancdefgh
You printed wrong format, try again
Enter persnr in the format of (YYYYMMDD): 20180912
Processsing...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=292202&siteId=1
Recommended