Results 1 to 5 of 5
- 02-13-2011, 10:19 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
program automatically chooses the default in switch inside loop
I'm stuck with my code, when i run this on the first run it is ok but when i '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.Java Code:import java.io.*; public class Store { public static void main(String args[]) throws Exception { char choice, another = ' ' ; int totalAmt, a, b, c, d, e; totalAmt = 0; a = 0; b = 0; c = 0; d = 0; e = 0; do { System.out.println("Computer Peripherals Shop"); System.out.println("A - Intel Core 2 Duo E7500: Php5,500"); System.out.println("B - Asus P5KPL-AM/PS G31 motherboard: Php2,750"); System.out.println("C - Kingston 1GB PC5300 DDR2 667 RAM: Php1,280"); System.out.println("D - Seagate Barracuda 250gb 7200rpm SATA Hard Drive: Php2,050"); System.out.println("E - 17inch LG 177WSB Widescreen Flatron LCD: Php4,980"); System.out.print("\nWhat do you want to buy: "); BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); choice = (char)br1.read(); switch(choice){ case 'A': a = 5500; totalAmt = totalAmt + a; System.out.println("Intel Core 2 Duo E7500: Php5,500"); System.out.println("\n\nTotal Amount: Php" + totalAmt); choice = ' '; break; case 'B': b = 2750; totalAmt = totalAmt + b; System.out.println("Asus P5KPL-AM/PS G31 motherboard: Php2,750"); System.out.println("\n\nTotal Amount: Php" + totalAmt); choice = ' '; break; case 'C': c = 1280; totalAmt = totalAmt + c; System.out.println("Kingston 1GB PC5300 DDR2 667 RAM: Php1,280"); System.out.println("\n\nTotal Amount: Php" + totalAmt); choice = ' '; break; case 'D': d = 2050; totalAmt = totalAmt + d; System.out.println("Seagate Barracuda 250gb 7200rpm SATA Hard Drive: Php2,050"); System.out.println("\n\nTotal Amount: Php" + totalAmt); choice = ' '; break; case 'E': e = 4980; totalAmt = totalAmt + e; System.out.println("17inch LG 177WSB Widescreen Flatron LCD: Php4,980"); System.out.println("\n\nTotal Amount: Php" + totalAmt); choice = ' '; break; default: System.out.println("Wrong choice!"); choice = ' '; } System.out.print("Do you want to buy another item? (y/n) "); BufferedReader br2 = new BufferedReader (new InputStreamReader(System.in)); another = (char)br2.read(); //choice = '' ; if (another=='y'){ another = ' '; } }while (another != 'n'); System.out.println("\nThe total amount is: Php" + totalAmt); System.out.println("\n\nThank you for buying."); } }
- 02-13-2011, 10:23 AM #2
It usually mistake in Java you can't compare string use == you must use .equals
Sorry I hastened to reply. Sure You can compare char use == . I think you have problem with space in "another" variable. You can tryJava Code:String value = "value"; if ("y".equals(value)) { }
Java Code:String value = br2.read().trim(); another = (char) value;
Last edited by Petr; 02-13-2011 at 10:28 AM.
Skype: petrarsentev
http://TrackStudio.com
- 02-13-2011, 10:31 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-13-2011, 11:28 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
How do I take the first line of the string? When it loops again, it automatically chooses default in the switch or sometimes skips
Here is the error output. The highlighted text is the error.Java Code:System.out.print("\nWhat do you want to buy: ");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) n
The total amount is: Php5500
Thank you for buying.
- 02-13-2011, 12:03 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
I think you mean 'first char of the String'. Have you read the API documentation for the String class? If so you must've read about the charAt(int p) method. It gets the character from the String at position p. Always read the API documentation before you ask questions.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Update JLabel during loop automatically
By carnado2008 in forum AWT / SwingReplies: 4Last Post: 01-22-2011, 02:55 PM -
Loop inside a switch
By mustachMan in forum New To JavaReplies: 3Last Post: 02-26-2010, 03:25 AM -
called external program does not automatically write file
By nickvandewiele in forum New To JavaReplies: 6Last Post: 02-25-2010, 02:38 PM -
Problem printing inside FOR loop
By cassysumandak in forum New To JavaReplies: 1Last Post: 10-04-2009, 05:02 PM -
println doesn't print from inside for loop, et.al.
By rdtindsm in forum New To JavaReplies: 5Last Post: 03-27-2009, 01:19 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks