is this the right way?

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-02-2012, 06:34 PM
lanmonster
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:
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));     } }```
Any help is much appreciated!
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
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```
Questions:
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.
• 12-02-2012, 06:53 PM
DarrylBurke
Re: is this the right way?
Please go through the Forum Rules -- particularly the third paragraph.

db
• 12-02-2012, 08:10 PM
Fubarable
Re: is this the right way?
You need to tell us exactly what is confusing you, exactly where you are stuck.
• 12-02-2012, 10:34 PM
lanmonster
Re: is this the right way?
Sorry, I'm a n00b. Does the code I ask for variables and manipulate them how I want?
• 12-03-2012, 03:49 AM
Darkzombies
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.
• 12-03-2012, 04:36 AM
lanmonster
Re: is this the right way?
OP updated. more code and questions at the bottom. Thanks for your responses!
• 12-03-2012, 04:55 AM
Darkzombies
Re: is this the right way?
You have to import before the class, also, I'll get you an example of what I meant.

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;         } }```
Sort of like that, also, I believe it should be JOptionPane, not JOption. Finally, you are declaring userInput every time you ask a question, do not declare it more than once. Don't use "String" in front, just do userInput = blahblahblah
• 12-03-2012, 05:30 AM
Fubarable
Re: is this the right way?
Quote:

Originally Posted by Darkzombies
You have to import before the class, also, I'll get you an example of what I meant.

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;         } }```
Sort of like that, also, I believe it should be JOptionPane, not JOption. Finally, you are declaring userInput every time you ask a question, do not declare it more than once. Don't use "String" in front, just do userInput = blahblahblah

Does this example compile?? You appear to be using a non-existing class, JOption, and appear to be re-declaring the same variable multiple times, both of which should cause this code to not make it past compilation.
• 12-03-2012, 05:35 AM
Darkzombies
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. >.<
• 12-03-2012, 06:22 AM
lanmonster
Re: is this the right way?
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));     } }```
compiles but when I ran it, no matter what I input, it says the value is zero. How do I take the userInput and manipulate it? I tried replacing hundred = hundred * 100 with hundred = userInput * 100 but it wont compile...
• 12-03-2012, 06:27 AM
Darkzombies
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...
• 12-03-2012, 06:31 AM
lanmonster
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?
• 12-04-2012, 03:28 PM
lanmonster
Re: is this the right way?
How do I change String userInput = JOptionPane.showInputDialog("How many 100's are there?"); to an int variable?
• 12-04-2012, 04:04 PM
Tolls
Re: is this the right way?
• 12-05-2012, 07:30 AM
lanmonster
Re: is this the right way?
Code:

```                int hundred = 0;         String userInput = JOptionPane.showInputDialog("How many 100's are there?");         Integer.parseInt(String userInput);         hundred = userInput * 100;```
When I compile, it says:
Code:

```Subway.java:7: ')' expected         Integer.parseInt(String userInput);                               ^ Subway.java:7: illegal start of expression         Integer.parseInt(String userInput);                                         ^```
What is wrong and how do I fix it?
• 12-05-2012, 07:41 AM
Fubarable
Re: is this the right way?
Code:

`Integer.parseInt(String userInput);`
That's not how you call a method. When you define a method, you have the parameter type, here String, in the parameter, but when you actually call the method, the type is not passed in. It's just called with Integer.parseInt(userInput). This is no different from the JOptionPane call above this line. You don't do it like:

Code:

```String userInputWrong = JOptionPane.showInputDialog(String "How many 100's are there?"); // *** incorrect *** String userInputRight  = JOptionPane.showInputDialog("How many 100's are there?"); // *** correct ***```
Right?

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.
• 12-05-2012, 03:39 PM
lanmonster
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?
• 12-05-2012, 03:57 PM
Tolls
Re: is this the right way?
Quote:

Originally Posted by lanmonster
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?

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.
Code:

```      BigDecimal bd1 = new BigDecimal(123);       BigDecimal bd2 = new BigDecimal(456);       BigDecimal bd3 = bd1.add(bd2);```
The call to add() on bd1 does not add the value of bd2 to bd1.
'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.
• 12-06-2012, 07:12 AM
lanmonster
Re: is this the right way?
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);```
is what I have. I get this error
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```
I really hope I am not bugging you guys...
• 12-06-2012, 07:49 AM
DarrylBurke
Re: is this the right way?
You need to look at the API for BigDecimal and see what parameters the divide(...) method accepts.

db
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last