Results 1 to 8 of 8
  1. #1
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default Writing a programs

    Hi
    I need some help. I'm creating a candystore program. I have all my values initialzed. I'm not sure how to get input and convert it properly so that I can add the total. could someone take a look and help me please?
    Java Code:
    import javax.swing.JOptionPane;
      public class Candyland2
    {
      public static void main (String []args)
      {
        
        String inputString;
        double dumdums = 1.50;
        double snickerballs = 3.00;
        double sweettarts = 2.00;
        double skittles = 2.75;
        double tax = .06;
        double total;
        double choice1;
        double choice2;
        double choice3;
        
       
        JOptionPane.showMessageDialog(null, "Welcome to CandyLand!");
        JOptionPane.showMessageDialog(null,"Items sold here: \n\t" 
                                        + "dumdums \n\tsnickerballs"
                                        + "\n\tsweettarts \n\tskittles");
        inputString = JOptionPane.showInputDialog ("What would you like first?");
            
        inputString = JOptionPane.showInputDialog ("What is your second choice?");
        
        
        inputString = JOptionPane.showInputDialog ("What is your third choice?");
        
        
        total = (choice1 + choice2 + choice3)*tax;
          
          JOptionPane.showMessageDialog (null, "Your total is: " + total + "Thank you for shop at CandyLand!");
        
          
      }
      }
    i would like to sell this candy by the pound. should i do that or would this make my program much more complicated at this point?
    Last edited by smray7; 10-26-2010 at 06:36 PM.

  2. #2
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default

    I doubt you would complicate it much more. You only need to multiply the price by the number of pounds(assuming the prices you specified are per pound).

    There are a few things I would like to point out:

    Where are choice 1, choice 2 and choice 3 initialised? I see input prompts but I don't see the entries into choice 1,2,3.

    Please change the prompts into:
    Java Code:
    choice1 = Double.parseDouble(JOptionPane.showInputDialog("Enter choice 1","")); //etc.
    And put your code in code tags

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bayan View Post
    Please change the prompts into:
    Java Code:
    choice1 = Double.parseDouble(JOptionPane.showInputDialog("Enter choice 1","")); //etc.
    Can a choice be, say, 2.718182? Just asking ...

    kind regards,

    Jos

  4. #4
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    thank you! i tried parsing before and i understand its use, i'm just confused on how to get user input.

    you see, when i run it...

    enter choice one:

    skittles [enter]

    my programs quits.

    how can i get choice1 to equal whatever option such as skittles so that i can calculate it. i'm not sure if this make sense because it seem that if i enter skittles, choice1 would parse skittles to a double which is 2.75 so i can calculate the total, but it doesn't work.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by smray7 View Post
    how can i get choice1 to equal whatever option such as skittles so that i can calculate it. i'm not sure if this make sense because it seem that if i enter skittles, choice1 would parse skittles to a double which is 2.75 so i can calculate the total, but it doesn't work.
    If you type, say, "dumdums" the Java virtual machine isn't so smart to associate it with the double value held by the variable dumdums. As a matter of fact variable names don't even exist at runtime. You have to make up your mind, either:

    a) the user types 0, 1, 2 or 3 for a valid choice and you use that int value as an index in an array that holds the prices for the corresponding candy, or

    b) you type in the name of the candy; the program has to compare what the user typed with all the valid candy names and find the corresponding price of the stuff.

    This may be a disappointment to you but that's the way Java works.

    kind regards,

    Jos

  6. #6
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    a) the user types 0, 1, 2 or 3 for a valid choice and you use that int value as an index in an array that holds the prices for the corresponding candy, or

    Jos
    hi, i know this post is old my my project is always a work in progress. anyhow...what does it mean to hold in an array? i'm still stuck tyring assign input to the right candy... my new code:

    Java Code:
    import javax.swing.JOptionPane;
    import java.text.*;
    import java.io.*;
    import java.util.*;
    
    public class Candyland9
    {
        static double dumdums;
        static double snickerballs = 3.00;
        static double sweettarts = 2.00;
        static double skittles = 2.75;
        static final  Double tax = .06;
        static double subtotal;
        static double total;
        static DecimalFormat decimalf = new DecimalFormat("$##.##");
        static NumberFormat money = NumberFormat.getCurrencyInstance(Locale.US);
        
        public static void main (String []args) throws IOException
        {
          double choice1;
          double choice2;
          double choice3;
          String input="";
          String input3="";
          File myFile = new File("E:\\project\\candy.txt"); // 1) open file
          Scanner inputFile = new Scanner(myFile).useDelimiter("\\s*:\\s*"); // 2) use scanner as input
          
        
          JOptionPane.showMessageDialog(null, "Welcome to CandyLand!");
          JOptionPane.showMessageDialog(null,"Items sold here: \n\t" 
                                        + "1)Dumdums \n\t2)Snickerballs"
                                        + "\n\t3)Sweettarts \n\t4)Skittles"
                                        + "\n\tPlease enter your choice by number.");
        
          while(inputFile.hasNext())
          {
            String inFile = inputFile.nextLine();// 2) read file
            System.out.print(inFile); // print out
          }
        
          input = JOptionPane.showInputDialog("Enter your first choice."); // need to somehow get user input and assign it to the right price
          choice1 = Double.parseDouble(input);
          input = JOptionPane.showInputDialog ("Enter a second choice or enter none");
         
          if(input.equalsIgnoreCase("none"))
          {
              total = calcTotal(choice1,0,0);
              displayMsg();
          }
       
          else         
          {
            input3 = JOptionPane.showInputDialog ("Enter a third choice or enter none?");
          }
          if(input3.equalsIgnoreCase("none"))
          {
             choice2 = Double.parseDouble(input);
             total = calcTotal(choice1,choice2,0);
             displayMsg();
          }
          else
          {
             choice2 = Double.parseDouble(input);// fix THIS 6 PLUS Tax IS NOT 7!!!
             choice3 = Double.parseDouble(input);
             total = calcTotal(choice1, choice2,choice3);
             displayMsg();
          }
                
          String morecandy;
          double totalcandy = 0;
          double morechoices = 0;
          String additional = JOptionPane.showInputDialog("For customers wanting additonal orders,"
                                         + "\n\tplease enter Y for yes or N for no.");
         
          if (additional.equalsIgnoreCase("Y"))
          {
              JOptionPane.showMessageDialog(null,"Items sold here: \n\t" 
                                                           + "1)Dumdums \n\t2)Snickerballs"
                                                           + "\n\t3)Sweettarts \n\t4)Skittles");
          do 
          {
              morecandy = JOptionPane.showInputDialog("Please make your selection by number."
                                                  + "\n\t Enter 0 when done.");              
              morechoices = Double.parseDouble(morecandy); // convert morecandy to useable number
              totalcandy += morechoices;
           }
                      
          while(morechoices != 0);
          
              total = calcTotal(totalcandy,0,0);
              displayMsg();
           
           }
          else
          {
               JOptionPane.showMessageDialog(null, "Goodbye!"); 
          }
                     
           inputFile.close(); //3) close file
         }
          public static double calcTotal(double one, double two, double three )
          {
              subtotal = (one + two + three) * tax;
              total = one + two + three + subtotal;
              return total;
          }
          public static void displayMsg()
          {
              JOptionPane.showMessageDialog(null, "Tax for all items is: " + decimalf.format(tax));
              JOptionPane.showMessageDialog(null,"Your total is: " + money.format (total));
              JOptionPane.showMessageDialog(null, "Thank you for shopping at CandyLand!\n\t"
                                               + "Tell a friend!");
          }
    }

  7. #7
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Talking

    patiently waiting

  8. #8
    bayan is offline Member
    Join Date
    Apr 2010
    Posts
    55
    Rep Power
    0

    Default

    He means enter in the question asking for the user input(the prompt text)
    Enter 0 for choice 1
    Enter 1 for choice 2 etc.

    Save the number the user enters, parse it into an int and use an array where arr[0]= choice 1 etc.

    Then you can take the amount to input in another prompt.

    If you don't understand what I'm trying to say please post again so I can explain

Similar Threads

  1. Listing Startup programs
    By priya_java in forum New To Java
    Replies: 2
    Last Post: 09-24-2010, 05:27 PM
  2. Putting Java Programs on a DVD
    By matt.monkeyboy in forum Advanced Java
    Replies: 4
    Last Post: 04-05-2010, 03:06 AM
  3. Replies: 1
    Last Post: 02-27-2010, 01:29 AM
  4. how can i package my programs for installation
    By buston01 in forum Advanced Java
    Replies: 1
    Last Post: 01-25-2010, 12:41 PM
  5. I need a simple programs
    By mikau in forum New To Java
    Replies: 2
    Last Post: 02-11-2008, 03:37 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
  •