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)
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.
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.
Code java:
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--
}
}
}
}
}
}
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.
Re: Storefront Exercise Troubles
Quote:
Originally Posted by
Norm
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??
Code :
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...
Re: Storefront Exercise Troubles
When you print out Strings for debugging you should add an ID before and a delimiter after:
Code :
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?