John
John

Reputation: 177

do while loop.. to proceed if correct input is given

I am trying to do a while loop but it kept failing on me. So while date is not entered by the user, this will keep running until a valid date is entered. isValidDate is a method to check whether the date is true or false. Date will only be true when enter dd/mm/yy.

I am trying to put a check on whether the date is correct or not. If it is correct, it will be stored into the String date and the program will continue. If it is incorrect, the program will then prompt the user again for the correct date.

When I first enter a incorrect date, it will then prompt me for the correct date to be entered again. At this point, when I enter a correct date, the program could not verify if it is a correct date or not anymore. Why is this happening?

Please help!

    public void bookingA(){
        bkList = new ArrayList<bookingInfo>();

        Scanner keys = new Scanner(System.in);
        String date;

        System.out.println("\nEnter the date: ");
        while(!isValidDate(keys.nextLine())) {
            do {
               System.out.println("That is not a valid date! Please enter again (dd/mm/yy): ");
               date = keys.nextLine();
            } while (!isValidDate(date));
            System.out.print(date);
            break;
        }
}

Upvotes: 2

Views: 6426

Answers (2)

Floris
Floris

Reputation: 46365

Every keys.nextLine takes another input. You have to read input once into a variable then refer to the variable! As it is you are reading input three times per inner loop.

EDIT based on the code you have after your edits, there is still a problem. You still have two places where you read the input - in the while loop, and in the condition. And you still have two while loops, when you need only one. I have deleted a few lines, and added an initial assignment to date before the loop begins. That gets you this:

public void bookingA(){
    bkList = new ArrayList<bookingInfo>();

    Scanner keys = new Scanner(System.in);
    String date;

    System.out.println("\nEnter the date: ");
    date = keys.nextLine();
    while(!isValidDate(date)) {
        System.out.println("That is not a valid date! Please enter again (dd/mm/yy): ");
        date = keys.nextLine();
    }
    System.out.print(date);

Note - I tried to maintain some of the struture of your code (namely, a while that actually tests a changing condition) but the answer given by Milos (which has an infinite loop that you break out of when the date is valid) is probably a better solution in general since you don't need to set the date to something before entering it. On the other hand you could say that in the code I suggested your assumption is that the user has entered a valid date, and the while loop is there to confirm it, and give another chance if the date proves not to be valid.

As you can see, both solutions work, but the philosophy of one is "I am putting you in this loop and I'm not letting you out until you enter a valid date", while the other is "I will keep checking that the date you entered is valid, until it is".

But both are a lot more readable than your original code. A simple task needs a simple code structure. Worth striving for.

Upvotes: 0

Miloš Lukačka
Miloš Lukačka

Reputation: 842

you have infinite cycle there, change it to:

System.out.println("\nEnter the date (dd/mm/yy): ");
while (true) {
    date = keys.nextLine();
    if (isValidDate(date)) {
         break;
    } else {
         System.out.println("That is not a valid date! Please enter again (dd/mm/yy): ");
    }
} 
System.out.print(date);

Upvotes: 3

Related Questions