Results 1 to 7 of 7
  1. #1
    sanjustified is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Please help Java small program

    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);
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Please help Java small program

    Please explain your problem in more detail. Are there any errors? If so please post the full text.

  3. #3
    2Pac is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Please help Java small program

    Java 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.
    Last edited by 2Pac; 11-12-2011 at 12:59 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Please help Java small program

    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.

  5. #5
    2Pac is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Please help Java small program

    Lol my bad, wasn't really paying attention. Edited it...

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Please help Java small program

    Why post code if you are not paying attention?

    At least you put code tags around it.

  7. #7
    2Pac is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Please help Java small program

    Did Michael Jordan ever miss a shot? Didn't know mistakes couldn't happen...

Similar Threads

  1. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 03:43 AM
  2. I need help doing a small program.
    By fastcar83 in forum New To Java
    Replies: 7
    Last Post: 11-03-2010, 10:00 PM
  3. Small Java program help
    By rawan in forum Java Applets
    Replies: 11
    Last Post: 03-27-2010, 06:43 PM
  4. Proofreading this small Java program
    By almina in forum New To Java
    Replies: 5
    Last Post: 10-23-2009, 07:42 AM
  5. Please HELP Java small program
    By afrttoh in forum New To Java
    Replies: 14
    Last Post: 11-08-2008, 02:29 AM

Posting Permissions

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