Gaetano
Gaetano

Reputation: 29

Getting the Code to Restart After Invalid Entry (Java)

I'm working on this block of code:

    while (!userCorrect) {
        Scanner input = new Scanner(System.in);
        System.out.print("Guessing (round " + roundNumber1++ + "): Choosing your letter from a-z: ");
            String letter = input.nextLine();
            if (letter.length () > 1) {
                System.out.println("You should not enter more than 1 character");
                } 


        System.out.println("end of (round " + roundNumber2++ + ")");
    }
    }

What I'm trying to do is after the user inputs more than 1 character, the code goes back to:

System.out.print("Guessing (round " + roundNumber1++ + "): Choosing your letter from a-z: ");

Until only 1 character is selected and then moves onto round 2.

Ex. assuming it's round 1 and the user inputs az. It'll throw my error saying that you should not enter more than 1 character and then it'll go back to:

Guessing(round 1): Choosing your character from a-z:

Any ideas? I got it to work but it's skipping to the next round and I want it to stay on the same round until a valid input. I've been searching for hours and can't get it to work.

Upvotes: 0

Views: 554

Answers (3)

Sabrina Tessier
Sabrina Tessier

Reputation: 79

Here's another way, using the continue keyword which basically forces execution back up to the top of the loop. Using this implementation, simply set done to true when you want it to be done.

    public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
            int roundNumber = 1;
            boolean done = false;

            do
            {
                System.out.println("Guessing (round " + roundNumber + "): Choosing your letter from a-z: ");
                String letter = input.nextLine();
                if(letter.length() > 1)
                {
                    System.out.println("You should not enter more than 1 character");
                    continue;
                }
                else
                {
                    roundNumber++;
                }

                //Set done to true based on some condition

            }while(!done);

        }
        //end main

Upvotes: 0

Mahamudul Hasan
Mahamudul Hasan

Reputation: 2823

you can use like below

while (!userCorrect) {
        Scanner input = new Scanner(System.in);
        System.out.print("Guessing (round " + roundNumber1 + "): Choosing your letter from a-z: ");
            String letter = input.nextLine();
            if (letter.length () > 1) {
                System.out.println("You should not enter more than 1 character");
                continue;
                } 

        System.out.println("end of (round " + roundNumber1 + ")");
        roundNumber1++;
    }

Upvotes: 0

Increment only after receiving valid input:

boolean userCorrect = false;
int roundNumber = 1;  // starting round

while (!userCorrect) {

    Scanner input = new Scanner(System.in);
    System.out.print("Guessing (round " + roundNumber + "): Choosing your letter from a-z: ");
    String letter = input.nextLine();

    if (letter.length () > 1) {
        System.out.println("You should not enter more than 1 character");
    }

    else{
        System.out.println("end of (round " + roundNumber + ")");
        roundNumber++;       // now increment
    }
}

Note this loop will continue until you set userCorrect = true somewhere within the loop.

Upvotes: 1

Related Questions