• 11-11-2011, 10:08 PM
sanjustified
Hi there,
I have to create a program ShoppingCalculator.java that allows the user to enter the prices of several items they have bought and then tells them the total amount they have spent. The user can enter the prices for as many items as they want, until they enter the letter ‘q’ to quit. The program should accept decimal values. The program should be created without using dialog box..

Unfortunately after trying to correct it for several times, I couldn't. Any help to fix this program would be very much appreciated :)
So far this is what I have done.
--------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.Scanner;
public class ShoppingCalculator
{
public static void main(String[] args)
{
double amount = 0.0;
Scanner scan = new Scanner(System.in);
String input = "";
while (true)
{
System.out.println("Enter the amount you spent on the next item, or 'q' to finish: ");
input = scan.nextLine();

if(input != null && input == "q") {
amount += Double.parseDouble(input);
}
else
amount ++;

}

System.out.println("Amount is £"+ amount);
}
}
• 11-12-2011, 12:36 AM
Norm
Please explain your problem in more detail. Are there any errors? If so please post the full text.
• 11-12-2011, 01:47 AM
2Pac
Code:

```import java.util.Scanner;  public class ShoppingCalculator  {  public static void main(String[] args)  {  boolean valid = true;  double amount = 0.0;  Scanner scan = new Scanner(System.in);  String input = "";  while ( valid == true )  { System.out.println("Enter the amount you spent on the next item, or 'q' to finish: ");  input = scan.nextLine();   if( input != null && input.equals( "q" ))   amount += Double.parseDouble(input);    else  amount ++; //add statement someone in here //so that valid becomes false at //some point   }   System.out.println("Amount is £"+ amount);  }  }```
I really have no idea what you us to help you with, but I see a couple of errors to begin with. First off your loop never ends because it always remains "true". That means it will never get to your print statement at the bottom. You should also use the String equals(), or equalsIgnoreCase() for comparing the strings. I've made some adjustments to your code but as the guy ahead of me said we really need more information on what you need help with.
• 11-12-2011, 01:56 AM
Norm
Quote:

input.equals( null )
I don't think the contents of a String object would be null. The String object reference variable: input could have a null value which you would test for with input == null.
• 11-12-2011, 01:59 AM
2Pac