Results 1 to 15 of 15
  1. #1
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default Please HELP Java small program

    Hello there,
    I need to do a small program which is:

    Write a program called ShoppingCalculator.java that allows the user to
    enter the prices of several items they have bought and then tells them the
    total amount. The user can enter the prices for as many items as they
    want, until they click Cancel. The program should then output the total
    amount they spent, in a message dialog box. Use pounds for all the
    amounts.

    Unfortunately, after spending many hours trying to figure out how to make this programme work correctly! I couldnít.
    I guess itís because I am a very beginner at java programming :-)
    I would really appreciate it if you could help me out.
    Thanks

    Here is what I have done so far:

    Java Code:
    import javax.swing.JOptionPane;
     
     
    public class ShoppingCalculator
     
    {
     
     
        public static void main(String[] args)
            {
     
                boolean done = false;
                double amount;
                String input;
                double total = 0;
     
                while (!done)
                    {
                        input = JOptionPane.showInputDialog("Enter the amount you spent on the next item:");
                        amount = Double.parseDouble (input);
               {
               if (input == null)total = total + amount;         
           }           
     
     
                   JOptionPane.showMessageDialog (null, "You spent a total amound of " + total  );
                    }
            }
    }

  2. #2
    jack239 is offline Member
    Join Date
    Jul 2008
    Posts
    35
    Rep Power
    0

    Default

    I could see you've written
    Java Code:
     if(input == null) total = total + amount
    I think it should be
    Java Code:
     if(input != null) total = total + amount
    because you'll add amount to total if and only if input has some value and not null
    New to Java/PHP/Javascript development?
    For free help go to- www.techcubetalk.com

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Actually you should deal with the input, null check.

    Java Code:
        public static void main(String[] args) {
            
            double amount = 0.0;
            
            while(true) {
                String str = JOptionPane.showInputDialog(null, "Enter the value");
                if(str != null) {
                    amount += Double.parseDouble(str);
                }
                else
                    break;
            }
            
            JOptionPane.showMessageDialog(null, "Amount is " + amount);
        }

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Some comments..

    I have some comments on your usage of curly brackets (or is it braces?)... anyway...
    Java Code:
              {
               if (input == null)total = total + amount;         
           }
    • The {} above, don't do anything (not needed, unless I missed their purpose)
    • Always use {} for if-else statements when (even if) it's a one-liner. This way you can always add code to your if-else statements without having to worry about remembering to add the {}.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by CJSLMAN View Post
    Always use {} for if-else statements when (even if) it's a one-liner. This way you can always add code to your if-else statements without having to worry about remembering to add the {}.
    I give full credit on this. It's a good practice actually. I've miss that. But in later once you get more familiar with programming those kind of things, simply you can avoid those braces. So it gives much clear view on your code.

  6. #6
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default thank you

    thank you so much , its working now ..:-)

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    If you solve the problem please mark it as solved lol. It's really helpful to all other members.

  8. #8
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    Here it is fully working:

    import javax.swing.JOptionPane;


    public class ShoppingCalculator

    {


    public static void main(String[] args) {

    double amount = 0.0;

    while(true) {
    String str = JOptionPane.showInputDialog(null, "Enter the value");
    if(str != null) {
    amount += Double.parseDouble(str);
    }
    else
    break;
    }

    JOptionPane.showMessageDialog(null, "Amount is £" + amount);
    }
    }

    Thanks alot Eranga :)
    Last edited by afrttoh; 11-08-2008 at 03:06 AM.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ya, that's what I explain in my code segment.

    By the way, are you working on Notepad? Why are you not using line indent there? If you use it, much easy to read the code.

  10. #10
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Ya, that's what I explain in my code segment.

    By the way, are you working on Notepad? Why are you not using line indent there? If you use it, much easy to read the code.
    I am using Editplus.
    is the line indent better?

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by afrttoh View Post
    I am using Editplus.
    is the line indent better?
    See the code segment I'm posting here. That's write on NetBeans and it has auto indenting facility. It's much easy to read, and it's much clear, isn't it?

  12. #12
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    yes , i think so , ill Google it and download it now .
    thank you once aging you have been very helpful!

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by afrttoh View Post
    ill Google it and download it now .
    What you are going to download? NetBeans?

  14. #14
    afrttoh is offline Member
    Join Date
    Nov 2008
    Location
    London
    Posts
    39
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    What you are going to download? NetBeans?
    Yes ,is it free to use?

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yep, it's free. If you want to work on with NetBeans download the latest version 6.1, there is another version 6.5 but it's a beta version.

    Here is the download link,

    Choose the correct one you need and download.

    Actually for line indenting you need to use NetBeans, you can use Tab space for that in editplus. Only thing is you should have a clear idea how to put tab spaces correctly.

Similar Threads

  1. Proofreading this small Java program
    By almina in forum New To Java
    Replies: 5
    Last Post: 10-23-2009, 08:42 AM
  2. java annotations - small prog
    By sebo in forum New To Java
    Replies: 5
    Last Post: 10-24-2008, 06:48 AM
  3. small issues with a program
    By jimJohnson in forum New To Java
    Replies: 6
    Last Post: 04-25-2008, 09:28 AM
  4. Small scale Java Editor
    By Greenfrog99 in forum AWT / Swing
    Replies: 0
    Last Post: 01-27-2008, 09:46 PM
  5. Small tennis simulation in Java
    By diego in forum New To Java
    Replies: 1
    Last Post: 12-02-2007, 02:32 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
  •