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

  • Thread starter Thread starter BrandNewDay
  • Start date Start date
  • Tags Tags
    Java
AI Thread Summary
The issue with the do-while loop not functioning as expected stems from how input is handled in Java. When using System.in.read(), the program reads a line of characters, capturing the first character input (like 'y') but also including the subsequent newline character from pressing Enter. This results in the loop not prompting for a new value of choice after the first iteration. To resolve this, it is recommended to use a Scanner object or a BufferedReader for input, which can more effectively manage user input and avoid issues with leftover characters in the input buffer. Additionally, flushing the input buffer may be necessary to ensure that only the intended character is processed in subsequent iterations.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

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