Results 1 to 20 of 31
Thread: is this the right way?
 12022012, 06:34 PM #1Member
 Join Date
 Dec 2012
 Posts
 22
 Rep Power
 0
Simple math program
This is a program I wrote to aide in doing a cash drop at my work. I need it to ask me for a variable and then manipulate it according to the monetary value. I am fairly certain I did it right. At the end I want it to:
 add all of the variables together \,
 display how much of each I have \,
 Display the total \,
 subtract 200 from the total and give me that value \,
 tell me how many of each bill to take out
This is the code I have thus far:
Java Code:public class Subway{ public static void main(String[] args) { import javax.swing.JOptionPane; //Gather Variables int hundred = 0; String userInput = JOption.showInputDialog("How many 100's are there?"); hundred = hundred * 100; int fifty = 0; String userInput = JOption.showInputDialog("How many 50's are there?"); fifty = fifty * 50; int twenty = 0; String userInput = JOption.showInputDialog("How many 20's are there?"); twenty = twenty * 20; int ten = 0; String userInput = JOption.showInputDialog("How many 10's are there?"); ten = ten * 10; int five = 0; String userInput = JOption.showInputDialog("How many 5's are there?"); five = five * 5; int one = 0; String userInput = JOption.showInputDialog("How many 1's are there?"); int quarter = 0; String userInput = JOption.showInputDialog("How many quarters are there?"); quarter = quarter / 4; int dime = 0; String userInput = JOption.showInputDialog("How many dimes are there?"); dime = dime / 10; int nickel = 0; String userInput = JOption.showInputDialog("How many nickels are there?"); nickel = nickel / 20; int penny = 0; String userInput = JOption.showInputDialog("How many pennies are there?"); penny = penny / 100; //Display each amount System.out.println("$ in hundreds: " + hundred); System.out.println("$ in fifties: " + fifty); System.out.println("$ in twenties: " + twenty); System.out.println("$ in tens: " + ten); System.out.println("$ in fives: " + five); System.out.println("$ in ones: " + one); System.out.println("$ in quarters: " + quarter); System.out.println("$ in dimes: " + dime); System.out.println("$ in nickels: " + nickel); System.out.println("$ in pennies: " + penny); //Add Variables System.out.println("Total is " + (hundred + fifty + twenty + ten + five + one + quarter + dime + nickel + penny)); //Subtract 200 System.out.println("Take out " + (hundred + fifty + twenty + ten + five + one + quarter + dime + nickel + penny  200)); } }
P.S. this is my first big java project!
*EDIT*
I decided to go with my gut and this is what I came up with. It gives me an error on compile though. the error is
Java Code:C:\Java>javac Subway.java Subway.java:2: error: illegal start of type import javax.swing.JOptionPane; ^ Subway.java:2: error: ';' expected import javax.swing.JOptionPane; ^ Subway.java:2: error: illegal start of type import javax.swing.JOptionPane; ^ Subway.java:2: error: ';' expected import javax.swing.JOptionPane; ^ Subway.java:2: error: <identifier> expected import javax.swing.JOptionPane; ^ Subway.java:4: error: illegal start of expression import javax.swing.JOptionPane; ^ Subway.java:4: error: not a statement import javax.swing.JOptionPane; ^ 7 errors
1. How do I get user input and store it as a variable?
2. How do I fix the errors I have on compiling?
3. Can anyone simplify what Darkzombies suggested? I'm a n00b and I don't understand what he said.Last edited by lanmonster; 12032012 at 06:40 AM.
 12022012, 06:53 PM #2
Re: is this the right way?
Please go through the Forum Rules  particularly the third paragraph.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.

Re: is this the right way?
You need to tell us exactly what is confusing you, exactly where you are stuck.
 12022012, 10:34 PM #4Member
 Join Date
 Dec 2012
 Posts
 22
 Rep Power
 0
Re: is this the right way?
Sorry, I'm a n00b. Does the code I ask for variables and manipulate them how I want?
 12032012, 03:49 AM #5Senior Member
 Join Date
 Nov 2012
 Posts
 105
 Rep Power
 0
Re: is this the right way?
You don't necessarily have to create variables for each calculation, you can simply create an array, and use each instance of the array in your calculations. I.E. Numbers[0] = Numbers[0] * 100 instead of hundred = hundred * 100 and so forth, it will still be understandable because of the prints. Then at the end, you can use a for loop and add all the instances of the array together.
 12032012, 04:36 AM #6Member
 Join Date
 Dec 2012
 Posts
 22
 Rep Power
 0
Re: is this the right way?
OP updated. more code and questions at the bottom. Thanks for your responses!
 12032012, 04:55 AM #7Senior Member
 Join Date
 Nov 2012
 Posts
 105
 Rep Power
 0
Re: is this the right way?
You have to import before the class, also, I'll get you an example of what I meant.
Java Code:import java.util.*; import javax.*; import java.awt.*; public class arrayTest{ public static void main(String[] args){ int[] numbers; String userInput = JOption.showInputDialog("How many 50's are there?"); numbers[0] *= 50; String userInput = JOption.showInputDialog("How many 20's are there?"); numbers[1] *= 20; String userInput = JOption.showInputDialog("How many 10's are there?"); numbers[2] *= 10; String userInput = JOption.showInputDialog("How many 5's are there?"); numbers[3] *= 5; String userInput = JOption.showInputDialog("How many 1's are there?"); numbers[4] *= 1; String userInput = JOption.showInputDialog("How many quarters are there?"); numbers[5] /= 4; String userInput = JOption.showInputDialog("How many dimes are there?"); numbers[6] /= 10; String userInput = JOption.showInputDialog("How many nickels are there?"); numbers[7] /= 20; String userInput = JOption.showInputDialog("How many pennies are there?"); numbers[8] /= 100; } }
Last edited by Darkzombies; 12032012 at 05:07 AM.

Re: is this the right way?
 12032012, 05:35 AM #9Senior Member
 Join Date
 Nov 2012
 Posts
 105
 Rep Power
 0
Re: is this the right way?
Well, I just used her code as an example, and explained both those things to him... Either way it wouldn't compile anyways because I have to initialize the Array, which I forgot to do. >.<
 12032012, 06:22 AM #10Member
 Join Date
 Dec 2012
 Posts
 22
 Rep Power
 0
Re: is this the right way?
Java Code:import javax.swing.JOptionPane; public class Subway{ public static void main(String[] args) { //Gather Variables int hundred = 0; String userInput = JOptionPane.showInputDialog("How many 100's are there?"); hundred = hundred * 100; int fifty = 0; userInput = JOptionPane.showInputDialog("How many 50's are there?"); fifty = fifty * 50; int twenty = 0; userInput = JOptionPane.showInputDialog("How many 20's are there?"); twenty = twenty * 20; int ten = 0; userInput = JOptionPane.showInputDialog("How many 10's are there?"); ten = ten * 10; int five = 0; userInput = JOptionPane.showInputDialog("How many 5's are there?"); five = five * 5; int one = 0; userInput = JOptionPane.showInputDialog("How many 1's are there?"); int quarter = 0; userInput = JOptionPane.showInputDialog("How many quarters are there?"); quarter = quarter / 4; int dime = 0; userInput = JOptionPane.showInputDialog("How many dimes are there?"); dime = dime / 10; int nickel = 0; userInput = JOptionPane.showInputDialog("How many nickels are there?"); nickel = nickel / 20; int penny = 0; userInput = JOptionPane.showInputDialog("How many pennies are there?"); penny = penny / 100; //Display each amount System.out.println("$ in hundreds: " + hundred); System.out.println("$ in fifties: " + fifty); System.out.println("$ in twenties: " + twenty); System.out.println("$ in tens: " + ten); System.out.println("$ in fives: " + five); System.out.println("$ in ones: " + one); System.out.println("$ in quarters: " + quarter); System.out.println("$ in dimes: " + dime); System.out.println("$ in nickels: " + nickel); System.out.println("$ in pennies: " + penny); //Add Variables System.out.println("Total is " + (hundred + fifty + twenty + ten + five + one + quarter + dime + nickel + penny)); //Subtract 200 System.out.println("Take out " + (hundred + fifty + twenty + ten + five + one + quarter + dime + nickel + penny  200)); } }
 12032012, 06:27 AM #11Senior Member
 Join Date
 Nov 2012
 Posts
 105
 Rep Power
 0
Re: is this the right way?
I know you're probably going to facepalm, but if hundred = 0, and you times 0 by 100, you still get 0...
 12032012, 06:31 AM #12Member
 Join Date
 Dec 2012
 Posts
 22
 Rep Power
 0
Re: is this the right way?
I know, but I thought the String userInput = JOptionPane.showInputDialog("How many 100's are there?"); junk changed hundred to whatever I input. How do I make it change?
 12042012, 03:28 PM #13Member
 Join Date
 Dec 2012
 Posts
 22
 Rep Power
 0
Re: is this the right way?
How do I change String userInput = JOptionPane.showInputDialog("How many 100's are there?"); to an int variable?
 12042012, 04:04 PM #14Moderator
 Join Date
 Apr 2009
 Posts
 12,401
 Rep Power
 21
Re: is this the right way?
Please do not ask for code as refusal often offends.
** This space for rent **
 12052012, 07:30 AM #15Member
 Join Date
 Dec 2012
 Posts
 22
 Rep Power
 0
Re: is this the right way?
Java Code:int hundred = 0; String userInput = JOptionPane.showInputDialog("How many 100's are there?"); Integer.parseInt(String userInput); hundred = userInput * 100;
Java Code:Subway.java:7: ')' expected Integer.parseInt(String userInput); ^ Subway.java:7: illegal start of expression Integer.parseInt(String userInput); ^

Re: is this the right way?
Java Code:Integer.parseInt(String userInput);
Java Code:String userInputWrong = JOptionPane.showInputDialog(String "How many 100's are there?"); // *** incorrect *** String userInputRight = JOptionPane.showInputDialog("How many 100's are there?"); // *** correct ***
Next you will have to do something with the int returned by this method such as put it into a variable. Currently you're just discarding it, ignoring it. So put it on the right side of an assignment statement and have an int variable on the left side ready to receive the int returned, just like the String variable in the JOptionPane line above it.Last edited by Fubarable; 12052012 at 07:44 AM.
 12052012, 03:39 PM #17Member
 Join Date
 Dec 2012
 Posts
 22
 Rep Power
 0
Re: is this the right way?
Thanks! You guys are great! I did some googling and found that BigDecimal is the best to use for currency but how do I use it and how do I operate mathematically on it?
 12052012, 03:57 PM #18Moderator
 Join Date
 Apr 2009
 Posts
 12,401
 Rep Power
 21
Re: is this the right way?
The key thing to remember is that a BigDecimal object is immutable.
That is it cannot change.
So all the methods on it return a new BigDecimal that is the result of the method.
Java Code:BigDecimal bd1 = new BigDecimal(123); BigDecimal bd2 = new BigDecimal(456); BigDecimal bd3 = bd1.add(bd2);
'bd1' is still 123.
But it returns a new BigDecimal that is the result, 579...which I assign to bd3.
I could have assigned it back to bd1 instead, if I didn't actually need to keep a copy of bd1 around.Please do not ask for code as refusal often offends.
** This space for rent **
 12062012, 07:12 AM #19Member
 Join Date
 Dec 2012
 Posts
 22
 Rep Power
 0
Re: is this the right way?
Java Code:userInput = JOptionPane.showInputDialog("How many quarters are there?"); int quarter = Integer.parseInt(userInput); int f = 4; BigDecimal bd = new BigDecimal(quarter); BigDecimal bd25 = bd.divide(f, 2, BigDecimal.ROUND_HALF_UP);
Java Code:Subway.java:33: cannot find symbol symbol : method divide(int,int,int) location: class java.math.BigDecimal BigDecimal bd25 = bd.divide(f, 2, BigDecimal.ROUND_HALF_UP); ^ 1 error
 12062012, 07:49 AM #20
Bookmarks