Results 1 to 8 of 8
Thread: Writing a programs
- 10-26-2010, 05:09 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
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?
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?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!"); } }Last edited by smray7; 10-26-2010 at 06:36 PM.
- 10-26-2010, 05:41 PM #2
Member
- Join Date
- Apr 2010
- Posts
- 55
- Rep Power
- 0
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:
And put your code in code tagsJava Code:choice1 = Double.parseDouble(JOptionPane.showInputDialog("Enter choice 1","")); //etc.
- 10-26-2010, 05:57 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 10-26-2010, 06:13 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
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.
- 10-26-2010, 06:41 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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
- 11-20-2010, 06:19 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
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!"); } }
- 11-23-2010, 03:28 AM #7
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
patiently waiting
- 04-28-2011, 07:49 AM #8
Member
- Join Date
- Apr 2010
- Posts
- 55
- Rep Power
- 0
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
-
Listing Startup programs
By priya_java in forum New To JavaReplies: 2Last Post: 09-24-2010, 05:27 PM -
Putting Java Programs on a DVD
By matt.monkeyboy in forum Advanced JavaReplies: 4Last Post: 04-05-2010, 03:06 AM -
Im writing to a file and i want to skip lines while writing to a text file.
By Broden_McDonald in forum New To JavaReplies: 1Last Post: 02-27-2010, 01:29 AM -
how can i package my programs for installation
By buston01 in forum Advanced JavaReplies: 1Last Post: 01-25-2010, 12:41 PM -
I need a simple programs
By mikau in forum New To JavaReplies: 2Last Post: 02-11-2008, 03:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks