Results 1 to 5 of 5
  1. #1
    jethph is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default 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:

    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.");
    		
    	}
    }
    please help. Thanks.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    It usually mistake in Java you can't compare string use == you must use .equals
    Java Code:
    String value = "value";
    if ("y".equals(value)) {
    }
    Sorry I hastened to reply. Sure You can compare char use == . I think you have problem with space in "another" variable. You can try
    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

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Petr View Post
    It usually mistake in Java you can't compare string use == you must use .equals
    Java Code:
    String value = "value";
    if ("y".equals(value)) {
    }
    Sorry I hastened to reply. Sure You can compare char use == . I think you have problem with space in "another" variable. You can try
    Java Code:
    String value = br2.read().trim();
    another = (char) value;
    The OP is comparing chars just fine; the OP forgot to read the end of line character(s); it is better to take the first char of the String returned by the readLine() method (it reads the new line character(s) as well).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    jethph is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    How do I take the first line of the string? When it loops again, it automatically chooses default in the switch or sometimes skips
    Java Code:
    System.out.print("\nWhat do you want to buy: ");
    Here is the error output. The highlighted text is the error.
    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.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jethph View Post
    How do I take the first line of the string?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Update JLabel during loop automatically
    By carnado2008 in forum AWT / Swing
    Replies: 4
    Last Post: 01-22-2011, 02:55 PM
  2. Loop inside a switch
    By mustachMan in forum New To Java
    Replies: 3
    Last Post: 02-26-2010, 03:25 AM
  3. called external program does not automatically write file
    By nickvandewiele in forum New To Java
    Replies: 6
    Last Post: 02-25-2010, 02:38 PM
  4. Problem printing inside FOR loop
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 10-04-2009, 05:02 PM
  5. println doesn't print from inside for loop, et.al.
    By rdtindsm in forum New To Java
    Replies: 5
    Last Post: 03-27-2009, 01:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •