Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Storefront Exercise Troubles

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Storefront Exercise Troubles

    import java.text.NumberFormat;
    import java.util.Locale;
    import java.util.Scanner; //Scanner Class Available

    public class Pies {

    /**
    * @param args
    */
    public static void main(String[] args) {

    Scanner inScan = new Scanner(System.in);
    NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US);
    //
    while (true)
    {
    System.out.println("Is there another customer waiting in line?");
    String newCustomer = inScan.nextLine();
    while (newCustomer.equalsIgnoreCase("Y") || newCustomer.equalsIgnoreCase("Yes"))
    {
    System.out.println("Are you a Pi Card Member?");
    String cardHolder = inScan.nextLine();
    while (cardHolder.equalsIgnoreCase("Y") || cardHolder.equalsIgnoreCase("Yes"))
    {
    System.out.println("Here's a list of products for sale:");
    System.out.println("(a) Cheese Pizza $10.00");
    System.out.println("(b) Pepperoni Pizza $10.00");
    System.out.println("(c) Cherry Pies $ 8.00");
    System.out.println("(d) Cherry Pie Slices $ 1.75");
    System.out.println("(e) Pi Charms (14k Gold) $45.00");
    System.out.println("(f) Checkout");
    System.out.println(System.getProperty("line.separa tor"));
    System.out.println("Which item would you like to purchase? (a,b,c,d, or e)");
    String addCart = inScan.nextLine();
    if (addCart.equalsIgnoreCase("A"))
    {
    System.out.println("How many Cheese Pizzas would you like to purchase?");
    double numberCheesePizzas = inScan.nextDouble();
    double cheesePizzaCost = 10.00;
    double cheeseTotal = (cheesePizzaCost * numberCheesePizzas);
    System.out.println("You have a total of " + numberCheesePizzas + " Cheese Pizzas in your cart for a total of $" + cheeseTotal + ".");
    System.out.println(System.getProperty("line.separa tor"));
    }
    if (addCart.equalsIgnoreCase("B"))
    {
    //and so on I didn't want to copy the entire code when i really just need help with making the individual loops work--
    }

    }

    }

    }

    }

    }


    The goal of the entire exercise is to allow multiple items to be put into a "basket", purchased, with sales tax, etc. None of that is important right now but can someone help me understand why the output for this using (a) and any number causes the loop to output the list of items and prices twice? ITS KILLING ME INSIDE TRYING TO FIGURE THIS OUT!

    Sample Output:

    Is there another customer waiting in line?
    Y
    Are you a Pi Card Member?
    Y
    Here's a list of products for sale:
    (a) Cheese Pizza $10.00
    (b) Pepperoni Pizza $10.00
    (c) Cherry Pies $ 8.00
    (d) Cherry Pie Slices $ 1.75
    (e) Pi Charms (14k Gold) $45.00
    (f) Checkout


    Which item would you like to purchase? (a,b,c,d, or e)
    A
    How many Cheese Pizzas would you like to purchase?
    23
    You have a total of 23.0 Cheese Pizzas in your cart for a total of $230.0.


    Here's a list of products for sale:
    (a) Cheese Pizza $10.00
    (b) Pepperoni Pizza $10.00
    (c) Cherry Pies $ 8.00
    (d) Cherry Pie Slices $ 1.75
    (e) Pi Charms (14k Gold) $45.00
    (f) Checkout


    Which item would you like to purchase? (a,b,c,d, or e)
    Here's a list of products for sale:
    (a) Cheese Pizza $10.00
    (b) Pepperoni Pizza $10.00
    (c) Cherry Pies $ 8.00
    (d) Cherry Pie Slices $ 1.75
    (e) Pi Charms (14k Gold) $45.00
    (f) Checkout


    Which item would you like to purchase? (a,b,c,d, or e)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Storefront Exercise Troubles

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting. Without formatting I can't see the nesting logic.

    Play computer with the code to see why it executes the way it does. Take a piece of paper and write down the values of the variables as you step through the code and see what the code is doing with those variables. Also as an aid you could add some println statements to print out the values of the variables used to control the looping.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Storefront Exercise Troubles

    Sorry about the poorly posted code. In regards to your advice of putting it down on paper i already have as well as just made small changes to the code to see what's wrong but to no avail.

    import java.text.NumberFormat;
    import java.util.Locale;
    import java.util.Scanner;	//Scanner Class Available
     
    public class Pies {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
     
    		Scanner inScan = new Scanner(System.in);
    		NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US);	
    		//
    		while (true)
    			{
    			System.out.println("Is there another customer waiting in line?");
    			String newCustomer = inScan.nextLine();
    			while (newCustomer.equalsIgnoreCase("Y") || newCustomer.equalsIgnoreCase("Yes"))
    				{
    					System.out.println("Are you a Pi Card Member?");
    					String cardHolder = inScan.nextLine();
    					while (cardHolder.equalsIgnoreCase("Y") || cardHolder.equalsIgnoreCase("Yes"))
    						{
    							String addCart ="x";
    							System.out.println("Here's a list of products for sale:");
    							System.out.println("(a) Cheese Pizza				$10.00");
    							System.out.println("(b) Pepperoni Pizza				$10.00");
    							System.out.println("(c) Cherry Pies					$ 8.00");
    							System.out.println("(d) Cherry Pie Slices				$ 1.75");	
    							System.out.println("(e) Pi Charms (14k Gold)			$45.00");
    							System.out.println("(f) Checkout");
    							System.out.println(System.getProperty("line.separator"));
    							System.out.println("Which item would you like to purchase? (a,b,c,d, or e)");
    							addCart = inScan.nextLine();
    						if (addCart.equalsIgnoreCase("A"))
    							{
    								System.out.println("How many Cheese Pizzas would you like to purchase?");
    								double numberCheesePizzas = inScan.nextDouble();
    								double cheesePizzaCost = 10.00;
    								double cheeseTotal = (cheesePizzaCost * numberCheesePizzas);
    								System.out.println("You have a total of " + numberCheesePizzas + " Cheese Pizzas in your cart for a total of $" + cheeseTotal + ".");
    								System.out.println(System.getProperty("line.separator"));
    							}
    						if (addCart.equalsIgnoreCase("B"))	
    							{
    								//and so on I didn't want to copy the entire code when i really just need help with making the individual loops work--
    							}
     
    						}
     
    				}
     
    		}
     
    	}
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Storefront Exercise Troubles

    One problem is with the way the Scanner class works. When it reads a whole line into its buffer that includes the lineend character. The nextLine() method removes everything from the buffer including the lineend character. The nextDouble() method does not remove the lineend character from the buffer so that the next call to nextLine() returns nothing as it removes the lineend from the buffer.
    To see this add a System.out.println("an ID "+<THESTRINGHERE> + "<");
    that prints out what is called after the call to nextLine() following the call to nextDouble().

    Did you play computer with the code to see how it executed? If that did not show you the problem, try debugging the code by adding println statements to it that print out the values of the variables that control the execution flow (for example those used in the while statements) so you can see what the computer sees as it executes.
    Last edited by Norm; September 16th, 2012 at 02:39 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Asmith1292 (September 16th, 2012)

  6. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Storefront Exercise Troubles

    Quote Originally Posted by Norm View Post
    One problem is with the way the Scanner class works. When it reads a whole line into its buffer that includes the lineend character. The nextLine() method removes everything from the buffer including the lineend character. The nextDouble() method does not remove the lineend character from the buffer so that the next call to nextLine() returns nothing as it removes the lineend from the buffer.
    To see this add a System.out.println("an ID "+<THESTRINGHERE> + "<");
    that prints out what is called after the call to nextLine() following the call to nextDouble().
    I have gone back through and attempted to see what the computer is seeing, but for the life of me I can not figure out why it System.out.println this section where it lists the items and prices twice! Does it have something to do with the System.out.println(System.getProperty("line.separa tor")) entry??
    import java.text.NumberFormat;
    import java.util.Locale;
    import java.util.Scanner;	//Scanner Class Available
     
    public class Pies {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
     
    		Scanner inScan = new Scanner(System.in);
    		NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US);	
    		//
    		while (true)
    			{
    			System.out.println("Is there another customer waiting in line?");
    			String newCustomer = inScan.nextLine();
    			System.out.println(newCustomer);
    			while (newCustomer.equalsIgnoreCase("Y") || newCustomer.equalsIgnoreCase("Yes"))
    				{
    					System.out.println("Are you a Pi Card Member?");
    					String cardHolder = inScan.nextLine();
    					//System.out.println(cardHolder);
    					while (cardHolder.equalsIgnoreCase("Y") || cardHolder.equalsIgnoreCase("Yes"))
    						{
    							String addCart ="";
    							System.out.println("Here's a list of products for sale:");
    							System.out.println("(a) Cheese Pizza				$10.00");
    							System.out.println("(b) Pepperoni Pizza				$10.00");
    							System.out.println("(c) Cherry Pies					$ 8.00");
    							System.out.println("(d) Cherry Pie Slices				$ 1.75");	
    							System.out.println("(e) Pi Charms (14k Gold)			$45.00");
    							System.out.println("(f) Checkout");
    							System.out.println(System.getProperty("line.separator"));
    							System.out.println("Which item would you like to purchase? (a,b,c,d, or e)");
    							addCart = inScan.nextLine();
    							//System.out.println(addCart);
    						if (addCart.equalsIgnoreCase("A"))
    							{
    								System.out.println("How many Cheese Pizzas would you like to purchase?");
    								double numberCheesePizzas = inScan.nextDouble();
    								//System.out.println(numberCheesePizzas);
    								double cheesePizzaCost = 10.00;
    								double cheeseTotal = (cheesePizzaCost * numberCheesePizzas);
    								System.out.println("You have a total of " + numberCheesePizzas + " Cheese Pizzas in your cart for a total of $" + cheeseTotal + ".");
    								System.out.println(System.getProperty("line.separator"));
    							}
    						if (addCart.equalsIgnoreCase("B"))	
    							{
    								//and so on I didn't want to copy the entire code when i really just need help with making the individual loops work--
    							}
     
    						}
     
    				}
     
    		}
     
    	}
     
    }

    I'm not sure what else I can do because it's not that I don't understand how the while loop works, I just don't understand why the loop is activating twice without user input on what item should be purchased! Quite frustrating...

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Storefront Exercise Troubles

    When you print out Strings for debugging you should add an ID before and a delimiter after:
    System.out.println("nC="+newCustomer+"<");

    Can you post the console from when you execute the program?
    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.


    When will execution exit the inner while() loop?
    Last edited by Norm; September 17th, 2012 at 03:45 PM.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. exercise solution ???
    By somebodyonearth in forum Java Theory & Questions
    Replies: 1
    Last Post: March 11th, 2012, 12:52 PM
  2. Exercise
    By phite2009 in forum Member Introductions
    Replies: 3
    Last Post: September 30th, 2011, 08:51 AM
  3. Is this what my exercise want?
    By Arkeshen in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2011, 04:51 PM
  4. JAVA exercise
    By gmilan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 13th, 2010, 02:30 PM
  5. Exercise problems
    By Virgildonatifan in forum Java Theory & Questions
    Replies: 0
    Last Post: February 14th, 2010, 04:12 AM