• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

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

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'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.
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 77159
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 77159
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no, i only hit enter once after 'y'
 
Greenhorn
Posts: 20
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Friend,
i go with your code.its works fine for me.
Also check the output generated by the program.
snap.jpeg
[Thumbnail for snap.jpeg]
Out put
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It also runs fine in command prompt, I think its an IDE issue on JCreator, am I right?
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic