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

  • Context: Java 
  • Thread starter Thread starter BrandNewDay
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
SUMMARY

The issue with the do-while loop in the provided Java program arises from the use of System.in.read(), which captures the first character input but leaves the newline character in the input buffer. This causes the loop to skip the prompt for a new value of choice after the first iteration. To resolve this, developers should utilize the Scanner class or BufferedReader for input handling, ensuring that the input buffer is properly managed to avoid capturing unwanted characters.

PREREQUISITES
  • Understanding of Java programming language
  • Familiarity with do-while loops in Java
  • Knowledge of input handling in Java using System.in
  • Experience with Java classes such as Scanner and BufferedReader
NEXT STEPS
  • Learn how to use the Scanner class for input in Java
  • Explore BufferedReader for efficient input handling in Java
  • Investigate input buffer management techniques in Java
  • Review best practices for using loops in Java programming
USEFUL FOR

Java developers, programming students, and anyone troubleshooting input handling issues in Java applications.

BrandNewDay
Messages
1
Reaction score
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
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.
 
I'd either use the Scanner object or a BufferedReader. But if not, you probably need to Flush the input buffer in there somewhere.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 3 ·
Replies
3
Views
4K