What's Causing the Do-While Loop to Fail in JAVA Program?

  • Java
  • Thread starter BrandNewDay
  • Start date
  • Tags
    Java
In summary, the main method contains a do-while loop that is supposed to execute repeatedly if the user inputs 'y'. However, the loop is not working as intended and instead, the program only displays the message "TOSS COIN? Press y for yes." without prompting for a new value of choice. It is suggested to use the Scanner or BufferedReader object to properly read the user's input, or to flush the input buffer.
  • #1
BrandNewDay
1
0
I don't know why in the main method, the do-while loop won't work the way it's supposed to.
For instance, if the user types in y, the first loop would execute and then since the value of choice is still 'y', it's supposed to loop again. But instead, it just displays "TOSS COIN? Press y for yes." without asking for a new value of choice. What's wrong with it?

Code:
do{
            System.out.print("TOSS COIN? Press y for yes.");
            choice = (char)System.in.read();
            if(choice == 'y' || choice == 'Y')
            {
                Coins c = Coins.flip();
                System.out.println(c);
                if(c == Coins.HEAD)
                    heads++;
                else
                    tails++;
            }
        }while(choice == 'y' || choice == 'Y');
 
Technology news on Phys.org
  • #2
BrandNewDay said:
I don't know why in the main method, the do-while loop won't work the way it's supposed to.
For instance, if the user types in y, the first loop would execute and then since the value of choice is still 'y', it's supposed to loop again. But instead, it just displays "TOSS COIN? Press y for yes." without asking for a new value of choice. What's wrong with it?

Code:
do{
            System.out.print("TOSS COIN? Press y for yes.");
            choice = (char)System.in.read();
            if(choice == 'y' || choice == 'Y')
            {
                Coins c = Coins.flip();
                System.out.println(c);
                if(c == Coins.HEAD)
                    heads++;
                else
                    tails++;
            }
        }while(choice == 'y' || choice == 'Y');

Welcome to PF, BrandNewDay! :smile:

Did you press the [Enter] key after pressing 'y'?

When you call System.in.read() you do not get just one character, but you get a line of characters of which you only read the first one.
As it is, you automatically get the second choice, which is the [Enter] character.
 
  • #3
I'd either use the Scanner object or a BufferedReader. But if not, you probably need to Flush the input buffer in there somewhere.
 

FAQ: What's Causing the Do-While Loop to Fail in JAVA Program?

1. Why is my Java loop not working?

There could be several reasons for a Java loop not working. Some common causes include incorrect syntax, logical errors, or not properly updating the loop control variable.

2. How do I fix a Java loop that is stuck in an infinite loop?

To fix an infinite loop, you can try breaking out of the loop using a conditional statement or using a break or continue statement within the loop. You can also check if the loop control variable is being updated correctly.

3. Why is my Java loop only running once?

This could be due to incorrect initialization or updating of the loop control variable. Make sure the loop condition is also properly set to allow for multiple iterations.

4. How do I loop through an array in Java?

You can use a for loop or a while loop to iterate through an array in Java. Alternatively, you can also use the enhanced for loop (for-each loop) introduced in Java 5.

5. What is the difference between a for loop and a while loop in Java?

A for loop is typically used when you know the number of iterations beforehand, while a while loop is used when the number of iterations is not known and depends on a condition. Additionally, a for loop allows for more concise code, while a while loop can be more flexible.

Similar threads

Replies
2
Views
3K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
6
Views
4K
Replies
1
Views
4K
Replies
5
Views
2K
Replies
75
Views
5K
Replies
37
Views
4K
Back
Top