File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes java program automatically chooses the default in switch on the next loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java program automatically chooses the default in switch on the next loop" Watch "java program automatically chooses the default in switch on the next loop" New topic
Author

java program automatically chooses the default in switch on the next loop

Jeth Gamad
Greenhorn

Joined: May 07, 2010
Posts: 8
'm stuck with my code, when i run this on the first run it is ok but when i press 'y' to reloop the program, it automatically chooses the default in the switch and does not wait the for input. Here is the code:


Please help, thanks.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
I ran your program and could not reproduce the problem. That is, it looks like it works. Are you sure that you are testing with the latest compiled code?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37865
    
  22
Welcome to the Ranch

The read() method of System.in is not easy to use, but your app seems to work. It may be that you are entering something slightly different; that happened to me when I tried your program.
Jeth Gamad
Greenhorn

Joined: May 07, 2010
Posts: 8
Yes, i gave the latest compiled code. Try to run the program, try to choose y to reloop the program, it will not wait for your input for the switch and automatically choose the default.
Jeth Gamad
Greenhorn

Joined: May 07, 2010
Posts: 8
Here is the output when I ran the program:


Computer Peripherals Shop
A - Intel Core 2 Duo E7500: Php5,500
B - Asus P5KPL-AM/PS G31 motherboard: Php2,750
C - Kingston 1GB PC5300 DDR2 667 RAM: Php1,280
D - Seagate Barracuda 250gb 7200rpm SATA Hard Drive: Php2,050
E - 17inch LG 177WSB Widescreen Flatron LCD: Php4,980
What do you want to buy: A


Intel Core 2 Duo E7500: Php5,500
Total Amount: Php5500


Do you want to buy another item? (y/n) y



Computer Peripherals Shop
A - Intel Core 2 Duo E7500: Php5,500
B - Asus P5KPL-AM/PS G31 motherboard: Php2,750
C - Kingston 1GB PC5300 DDR2 667 RAM: Php1,280
D - Seagate Barracuda 250gb 7200rpm SATA Hard Drive: Php2,050
E - 17inch LG 177WSB Widescreen Flatron LCD: Php4,980
What do you want to buy: Wrong choice!

Do you want to buy another item? (y/n)



As you can see, when you choose 'y' for the second and nth times to reloop the program and display again the choices from switch, it automatically chooses the default in switch as highlighted with a textcolor and bold face, and does not wait for input to choose from the choices and jumps to
Do you want to buy another item? (y/n)

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37865
    
  22
What are you entering after the "y"? Are you pushing "enter" twice? I have just tried your code, and I suffered no errors.
Jeth Gamad
Greenhorn

Joined: May 07, 2010
Posts: 8
no, i only hit enter once after 'y'
Anshul K Jain
Greenhorn

Joined: Jul 24, 2010
Posts: 20

hi Friend,
i go with your code.its works fine for me.
Also check the output generated by the program.



[Thumbnail for snap.jpeg]

Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

In every loop of the program a BufferedReader based on System.in is created twice. These instances are never closed. I guess that creating more than one BufferedReader on a single input stream can generally lead to unpredictable (or at least hardly predictable) results, and I'd avoid it. My suggestion is to create a single instance of BufferedReader at the beginning of the program and use it to read all inputs.
Jeth Gamad
Greenhorn

Joined: May 07, 2010
Posts: 8
I think there is an issue in IDE that I am using. It runs fine in Eclipse, but the problem arises in JCreator, is my code has problems with JCreator?
Jeth Gamad
Greenhorn

Joined: May 07, 2010
Posts: 8
It also runs fine in command prompt, I think its an IDE issue on JCreator, am I right?
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

Jeth,

I guess that the difference between your environments is in the handling of input stream. If you try the suggestion I've hinted above, I'd be very interested in whether that would help. To elaborate on the suggestion:

1) Move the BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); statement from line 28 to before the while loop (before line 18)

2) Remove the BufferedReader br2 = new BufferedReader (new InputStreamReader(System.in)); statement from line 83 altogether

3) Use br1 instead of br2 on line 84.

Post back whether it helped, if you can. I'm genuinely curious
Jeth Gamad
Greenhorn

Joined: May 07, 2010
Posts: 8
I tried your method Martin, In Eclipse, when you choose on the choices, it displays the choices twice with a default choice on switch on the next displayed choices, like this:


Computer Peripherals Shop
A - Intel Core 2 Duo E7500: Php5,500
B - Asus P5KPL-AM/PS G31 motherboard: Php2,750
C - Kingston 1GB PC5300 DDR2 667 RAM: Php1,280
D - Seagate Barracuda 250gb 7200rpm SATA Hard Drive: Php2,050
E - 17inch LG 177WSB Widescreen Flatron LCD: Php4,980
What do you want to buy: A


Intel Core 2 Duo E7500: Php5,500
Total Amount: Php5500


Do you want to buy another item? (y/n)


Computer Peripherals Shop
A - Intel Core 2 Duo E7500: Php5,500
B - Asus P5KPL-AM/PS G31 motherboard: Php2,750
C - Kingston 1GB PC5300 DDR2 667 RAM: Php1,280
D - Seagate Barracuda 250gb 7200rpm SATA Hard Drive: Php2,050
E - 17inch LG 177WSB Widescreen Flatron LCD: Php4,980
What do you want to buy: Wrong choice!


Do you want to buy another item? (y/n) y



Computer Peripherals Shop
A - Intel Core 2 Duo E7500: Php5,500
B - Asus P5KPL-AM/PS G31 motherboard: Php2,750
C - Kingston 1GB PC5300 DDR2 667 RAM: Php1,280
D - Seagate Barracuda 250gb 7200rpm SATA Hard Drive: Php2,050
E - 17inch LG 177WSB Widescreen Flatron LCD: Php4,980
What do you want to buy: Wrong choice!


Do you want to buy another item? (y/n)


Computer Peripherals Shop
A - Intel Core 2 Duo E7500: Php5,500
B - Asus P5KPL-AM/PS G31 motherboard: Php2,750
C - Kingston 1GB PC5300 DDR2 667 RAM: Php1,280
D - Seagate Barracuda 250gb 7200rpm SATA Hard Drive: Php2,050
E - 17inch LG 177WSB Widescreen Flatron LCD: Php4,980
What do you want to buy: A


Intel Core 2 Duo E7500: Php5,500
Total Amount: Php11000


Do you want to buy another item? (y/n)


Computer Peripherals Shop
A - Intel Core 2 Duo E7500: Php5,500
B - Asus P5KPL-AM/PS G31 motherboard: Php2,750
C - Kingston 1GB PC5300 DDR2 667 RAM: Php1,280
D - Seagate Barracuda 250gb 7200rpm SATA Hard Drive: Php2,050
E - 17inch LG 177WSB Widescreen Flatron LCD: Php4,980
What do you want to buy: Wrong choice!


Do you want to buy another item? (y/n)



On JCreator, it skips asking
Do you want to buy another item? (y/n)
here is the output on JCreator:



--------------------Configuration: <Default>--------------------



Computer Peripherals Shop
A - Intel Core 2 Duo E7500: Php5,500
B - Asus P5KPL-AM/PS G31 motherboard: Php2,750
C - Kingston 1GB PC5300 DDR2 667 RAM: Php1,280
D - Seagate Barracuda 250gb 7200rpm SATA Hard Drive: Php2,050
E - 17inch LG 177WSB Widescreen Flatron LCD: Php4,980
What do you want to buy: A


Intel Core 2 Duo E7500: Php5,500
Total Amount: Php5500


Do you want to buy another item? (y/n)


Computer Peripherals Shop
A - Intel Core 2 Duo E7500: Php5,500
B - Asus P5KPL-AM/PS G31 motherboard: Php2,750
C - Kingston 1GB PC5300 DDR2 667 RAM: Php1,280
D - Seagate Barracuda 250gb 7200rpm SATA Hard Drive: Php2,050
E - 17inch LG 177WSB Widescreen Flatron LCD: Php4,980
What do you want to buy: A


Intel Core 2 Duo E7500: Php5,500
Total Amount: Php11000


Do you want to buy another item? (y/n)


Computer Peripherals Shop
A - Intel Core 2 Duo E7500: Php5,500
B - Asus P5KPL-AM/PS G31 motherboard: Php2,750
C - Kingston 1GB PC5300 DDR2 667 RAM: Php1,280
D - Seagate Barracuda 250gb 7200rpm SATA Hard Drive: Php2,050
E - 17inch LG 177WSB Widescreen Flatron LCD: Php4,980
What do you want to buy: A


Intel Core 2 Duo E7500: Php5,500
Total Amount: Php16500


Do you want to buy another item? (y/n)


Computer Peripherals Shop
A - Intel Core 2 Duo E7500: Php5,500
B - Asus P5KPL-AM/PS G31 motherboard: Php2,750
C - Kingston 1GB PC5300 DDR2 667 RAM: Php1,280
D - Seagate Barracuda 250gb 7200rpm SATA Hard Drive: Php2,050
E - 17inch LG 177WSB Widescreen Flatron LCD: Php4,980
What do you want to buy:

Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

Jeth,

then I'd suggest to replace
with
That will force the BufferedReader to consume the whole line that has been entered and next read should start at the beginning of a new line, which is what you actually want. As Campbell pointed out earlier, reading from System.in can be tricky.

Note: the concat(" ") call protects against exceptions that would arise if you hit just Enter (the line would have zero length and charAt(0) would fail). It is not particularly nice solution, but works.
Jeth Gamad
Greenhorn

Joined: May 07, 2010
Posts: 8
It worked. Thank you very much for all of your help. Does JCreator handles the inputstream differently? That is the IDE we are using at school.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

In my opinion the original version of your code was incorrect and that it worked somewhere is a coincidence. If you want the user to enter his choice and then hit Enter, you should explicitly read the whole line from input stream, as in the last version of the code.

Thought the problem was apparently more convoluted, one of the causes was creating two buffered readers on the same input stream. Buffered reader can - and generally will - read some characters ahead, and it might consume more characters from the original stream than you actually read from it. The position of the source stream therefore does not correspond to the number of characters read from the buffered reader, and subsequent reads from the input stream (direct or, as in your case, indirect via another reader) will read from different position than you'd expected.

This was probably complicated further by the peculiarities of the System.in, which I'm unable to say anything more of - I barely use System.in at all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java program automatically chooses the default in switch on the next loop
 
Similar Threads
executing cleartool from java
Java I/O for two file
Currency Problem + General Advice
do {} while statement illegal start of type
prog of multiple inputs