Search code examples
javajcreator

How to return back to a previous statement after invalid input instead of restarting the whole program in Java?


I'm writing this program where users can enter their name, identity card number, and desired membership level to sign up (Options available are only Gold, Platinum, Diamond). The program will then save the name, identity card number, and membership level in separate variables respectively. The format for the identity card number is set as YYMMDDPB###G, where the first 6 digits, YYMMDD indicates the date of birth (year-month-day), PB represents the place of birth, and the last digit G represents the gender (odd numbers represent male and even numbers represent female). I also need to allow the user to repeat the process for any invalid input.

For membership level, I want to allow only the letters G, P and, D for input, and other inputs will be considered invalid, prints an error statement, and brings back to the membership level input for the user to re-enter again. How do I do it?

public static void main(String[] args) {

        Scanner input = new Scanner (System.in);
        String prompt;

        do
            {
            Scanner sc = new Scanner(System.in);
            // Part 1 - Receiving input
            // Name
            System.out.println("Please enter your name: "); // Member enters name here
            String name = sc.nextLine();

            // Identity Card Number
            System.out.println("Please enter your IC number (without the dash): "); // Member enters identity card number here
            String identity_card_number = sc.nextLine();

            // Membership Level
            System.out.println("Please enter the membership level you would like to sign up. Enter G for Gold, P for Platinum, D for Diamond. (Enter only G, P, or D.)"); // Member enters desired membership level to sign up
            String membership_level = sc.nextLine();
            String memLvl = "default";

            if (membership_level.equals("G")){
                memLvl = ("Gold");
            }
            else if (membership_level.equals("P")){
                memLvl = ("Platinum");
            }
            else if (membership_level.equals("D")){
                memLvl = ("Diamond");
            }
            else {
                memLvl = ("UNINDENTIFIED: Please re-enter the correct input for desired membership level");
            }

Edit: To answer Chintan Adhia's answer, I made some changes to the code you suggested.

// Membership Level
            boolean isMemLvlInvalid = false;
            String memLvl = "default";
            String membership_level = sc.nextLine();
            do{
                    isMemLvlInvalid = false;
                    System.out.println("Please enter the membership level you would like to sign up. Enter G for Gold, P for Platinum, D for Diamond. (Enter only G, P, or D.)"); // Member enters desired membership level to sign up
                    membership_level = sc.nextLine();

                    if (membership_level.equals("G")) {
                    memLvl = ("Gold");
                    } else if (membership_level.equals("P")) {
                    memLvl = ("Platinum");
                    } else if (membership_level.equals("D")) {
                    memLvl = ("Diamond");
                    } else {
                    memLvl = ("UNINDENTIFIED: Please re-enter the correct input for desired membership level");
                    isMemLvlInvalid = true;
                    System.out.println(memLvl);
                    }
                }
        while(isMemLvlInvalid);

The problem with this code is that I have to press the Enter key twice after entering my identity card number (probably caused by sc.nextLine), any way to fix that so I just have to press the Enter key once?


Solution

  • One of the option is to run the Membership Level in loop till it's not correct. Basically what sittsering has said. Sample code is given below.

                boolean isMemLvlInalid = false;
            String memLvl = "default";
            do{
                isMemLvlInalid = false;
                System.out.println("Please enter the membership level you would like to sign up. Enter G for Gold, P for Platinum, D for Diamond. (Enter only G, P, or D.)"); // Member enters desired membership level to sign up
                String membership_level = sc.nextLine();
    
                if (membership_level.equals("G")) {
                    memLvl = ("Gold");
                } else if (membership_level.equals("P")) {
                    memLvl = ("Platinum");
                } else if (membership_level.equals("D")) {
                    memLvl = ("Diamond");
                } else {
                    memLvl = ("UNINDENTIFIED: Please re-enter the correct input for desired membership level");
                    isMemLvlInalid = true;
                    System.out.println(memLvl);
                }
            }
            while(isMemLvlInalid);