# is this the right way?

Show 40 post(s) from this thread on one page
Page 2 of 2 First 12
• 12-06-2012, 10:13 AM
JosAH
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?

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
• 12-07-2012, 03:17 AM
lanmonster
Re: is this the right way?
Did some reading, this code compiles but when I test it, it gives me the wrong answer :(
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();```
• 12-07-2012, 04:13 AM
faustnero
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:
Code:

`import java.util.Scanner;`
and then by creating a scanner:
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:
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
• 12-07-2012, 04:44 AM
lanmonster
Re: is this the right way?
Does this work independent of the command line? I was using JOptionPane so I could make an app...
• 12-07-2012, 10:38 AM
Tolls
Re: is this the right way?
Quote:

Originally Posted by faustnero
If you want get a user input from a user, you will have to import the 'Scanner' utility by:
Code:

`import java.util.Scanner;`
and then by creating a scanner:
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:
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...
• 12-07-2012, 10:41 AM
Tolls
Re: is this the right way?
Quote:

Originally Posted by lanmonster
Did some reading, this code compiles but when I test it, it gives me the wrong answer :(
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:
Code:

```        userInput = JOptionPane.showInputDialog("How many quarters are there?");         int quarters = Integer.parseInt(userInput);         int quartersAsCents = quarters * 25;```
• 12-07-2012, 03:15 PM
lanmonster
Re: is this the right way?
it gives me what I input...no calculations are done to it...
• 12-07-2012, 03:58 PM
Tolls
Re: is this the right way?
The following (which is the same as your code above, but with a fixed value for 'quarter'):
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);   }```
So I don't know what else is up with your code, because that bit works.
• 12-08-2012, 08:30 AM
lanmonster
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?
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);```
• 12-08-2012, 10:38 AM
DarrylBurke
Re: is this the right way?
Here's a hint: JOptionPane handles a String[] array (or any other array) really well.

db
• 12-08-2012, 05:12 PM
lanmonster
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?
Show 40 post(s) from this thread on one page
Page 2 of 2 First 12