# Thread: is this the right way?

1. ## Re: is this the right way?

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?
You don't need BigDecimals if you define a penny as a monetary unit (i.e. a penny is worth 'one' and a dollar is worth 'hundred').

kind regards,

Jos

2. Member
Join Date
Dec 2012
Posts
22
Rep Power
0

## Re: is this the right way?

Did some reading, this code compiles but when I test it, it gives me the wrong answer :(
Java Code:
```userInput = JOptionPane.showInputDialog("How many quarters are there?");
double quarter = Double.parseDouble(userInput);
BigDecimal q = new BigDecimal("4");
BigDecimal d6 = new BigDecimal(quarter);
BigDecimal d2 = d6.divide(q, 2, BigDecimal.ROUND_HALF_UP);
double quarters = d2.doubleValue();```
Last edited by lanmonster; 12-07-2012 at 03:22 AM.

3. Member
Join Date
Dec 2012
Posts
1
Rep Power
0

## Re: is this the right way?

If you want get a user input from a user, you will have to import the 'Scanner' utility by:
Java Code:
`import java.util.Scanner;`
and then by creating a scanner:
Java Code:
```// input is the new scanner name and you can name it whatever you prefer
Scanner input = new Scanner(System.in);```
and then putting the user input in a variable:
Java Code:
```// for example the user entry is an int and the variable name is number
// nextInt(); refers to the user entry after the 'enter' key is pressed.
int number = input.nextInt();```
This is the most simplest way I know how you can get inputs from users. Peace&Cheers

4. Member
Join Date
Dec 2012
Posts
22
Rep Power
0

## Re: is this the right way?

Does this work independent of the command line? I was using JOptionPane so I could make an app...

5. Moderator
Join Date
Apr 2009
Posts
12,962
Rep Power
22

## Re: is this the right way?

Originally Posted by faustnero
If you want get a user input from a user, you will have to import the 'Scanner' utility by:
Java Code:
`import java.util.Scanner;`
and then by creating a scanner:
Java Code:
```// input is the new scanner name and you can name it whatever you prefer
Scanner input = new Scanner(System.in);```
and then putting the user input in a variable:
Java Code:
```// for example the user entry is an int and the variable name is number
// nextInt(); refers to the user entry after the 'enter' key is pressed.
int number = input.nextInt();```
This is the most simplest way I know how you can get inputs from users. Peace&Cheers
They're using a JOptionPane, which is a perfectly reasonable way of doing this.
Seems a bit pointless to change it to a Scanner...

6. Moderator
Join Date
Apr 2009
Posts
12,962
Rep Power
22

## Re: is this the right way?

Originally Posted by lanmonster
Did some reading, this code compiles but when I test it, it gives me the wrong answer :(
Java Code:
```userInput = JOptionPane.showInputDialog("How many quarters are there?");
double quarter = Double.parseDouble(userInput);
BigDecimal q = new BigDecimal("4");
BigDecimal d6 = new BigDecimal(quarter);
BigDecimal d2 = d6.divide(q, 2, BigDecimal.ROUND_HALF_UP);
double quarters = d2.doubleValue();```
What results are you getting from that?

You might find it easier to go Jos' route of working in cents up until you give the final answer.
Skip the BigDecimal entirely.
So the above would be something like:
Java Code:
```        userInput = JOptionPane.showInputDialog("How many quarters are there?");
int quarters = Integer.parseInt(userInput);
int quartersAsCents = quarters * 25;```

7. Member
Join Date
Dec 2012
Posts
22
Rep Power
0

## Re: is this the right way?

it gives me what I input...no calculations are done to it...

8. Moderator
Join Date
Apr 2009
Posts
12,962
Rep Power
22

## Re: is this the right way?

The following (which is the same as your code above, but with a fixed value for 'quarter'):
Java Code:
```   public static void main(String args[]) {
double quarter = 1.0;
BigDecimal q = new BigDecimal("4");
BigDecimal d6 = new BigDecimal(quarter);
BigDecimal d2 = d6.divide(q, 2, BigDecimal.ROUND_HALF_UP);
double quarters = d2.doubleValue();
System.out.println(quarters);
}```
Gives the expected 0.25 answer.
So I don't know what else is up with your code, because that bit works.

9. Member
Join Date
Dec 2012
Posts
22
Rep Power
0

## Re: is this the right way?

Did away with BigDecimal and went with the suggested method. Got it working. Now, how do I get all of this on one JOptionPane?
Java Code:
```JOptionPane.showMessageDialog(null, "Money in hundreds: \$" + hundred);
JOptionPane.showMessageDialog(null, "Money in fifties: \$" + fifty);
JOptionPane.showMessageDialog(null, "Money in twenties: \$" + twenty);
JOptionPane.showMessageDialog(null, "Money in tens: \$" + ten);
JOptionPane.showMessageDialog(null, "Money in fives: \$" + five);
JOptionPane.showMessageDialog(null, "Money in ones: \$" + one);
JOptionPane.showMessageDialog(null, "Money in quarters: \$" + quarter);
JOptionPane.showMessageDialog(null, "Money in dimes: \$" + dime);
JOptionPane.showMessageDialog(null, "Money in nickels: \$" + nickel);
JOptionPane.showMessageDialog(null, "Money in pennies: \$" + penny);```

10. ## Re: is this the right way?

Here's a hint: JOptionPane handles a String[] array (or any other array) really well.

db

11. Member
Join Date
Dec 2012
Posts
22
Rep Power
0

## Re: is this the right way?

Alright! I got it working. What would be the best way to make it tell me how many of each bill to take? I don't need syntax, just an alg.
Also, how do I make it an app that will run on a computer without java jdk?
Last edited by lanmonster; 12-08-2012 at 05:21 PM.

Page 2 of 2 First 12

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•