'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:
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?
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
posted
0
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
posted
0
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: 32830
4
posted
0
What are you entering after the "y"? Are you pushing "enter" twice? I have just tried your code, and I suffered no errors.
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
posted
0
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
posted
0
It also runs fine in command prompt, I think its an IDE issue on JCreator, am I right?
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:
Post back whether it helped, if you can. I'm genuinely curious
Jeth Gamad
Greenhorn
Joined: May 07, 2010
Posts: 8
posted
0
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!
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:
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
posted
0
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.
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.