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 4 of 4

Thread: Help with my program please :) I can't figure this out.

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Help with my program please :) I can't figure this out.

    Java program is not calculating total?
    I have to write a program that prompts the user to make a choice of food then adds the total. The user needs to prompted 3 times.
    When I run the program it only gives me a total of zero. Why is it not calculating the total?
    import javax.swing.JOptionPane;
    public class FastFood
    {
    double total = 0;
    public static void main(String[] args)
    {
     
    double choice = getChoice();
    if (choice == 0)
    JOptionPane.showMessageDialog(null, "Your total is $0.00.");
    else 
    {
    calculateTotal(total, choice);
    choice = getChoice();
    if (choice == 0)
    JOptionPane.showMessageDialog(null, "Your Total is:" + total );
    else
    {
    calculateTotal(total, choice);
    choice = getChoice();
    if (choice == 0)
    JOptionPane.showMessageDialog(null, "Your Total is:" + total );
    else
    {
    calculateTotal(total, choice);
    JOptionPane.showMessageDialog(null, "Your Total is:" + total );
    }
    }
    }
    }
     
    public static double getChoice()
    {
    String input = JOptionPane.showInputDialog(null, "Choose your meal Item" +
    "\n(1)Burger $4.99" + "\n(2)Pepsi $2.00" + 
    "\n(3)Chips $0.75");
    return Double.parseDouble(input); 
    }
     
    public static void calculateTotal(double total, double choice)
    {
    if (choice == 1)
    total = total + 4.99;
    else if (choice == 2)
    total = total + 2.00;
    else if (choice == 3)
    total = total + 0.75;
    else
    JOptionPane.showMessageDialog(null, "Invalid choice enter numbers 0-3 only");
    }
    }
    Last edited by scholaryoshi; February 24th, 2012 at 01:01 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with my program please :) I can't figure this out.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    Why are you reading in the user's choice as a double and not as an int?

    To debug your code, add lots of printlns to the code to print out the values of ALL the variables as the code executes to show you what the program is doing as it executes. Be sure to add ID labels to all the printouts so you know what was printed.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with my program please :) I can't figure this out.

    Thanks for your reply. I appreciate the tip

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with my program please :) I can't figure this out.

    The formatting of your code with all the statements starting in the first column makes it hard to understand the logic.
    All code within a pair of {}s should be indented 3-4 spaces to make it easy to see the nesting of the logic.
    Continued lines should be indented so the continued part is obvious.

Similar Threads

  1. I can't figure out why i keep getting this error
    By chrissy2860 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 17th, 2011, 03:06 PM
  2. just one more error that i cannot figure out please help me.
    By knoxy5467 in forum What's Wrong With My Code?
    Replies: 31
    Last Post: June 14th, 2011, 09:04 AM
  3. [SOLVED] can't figure out..
    By darego in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 10th, 2010, 02:26 PM
  4. I'm new at this and can't figure it out
    By jaheh06 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 22nd, 2010, 08:44 AM
  5. [SOLVED] Java program to prompt and display the user input number as odd or even
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 22nd, 2009, 01:19 AM