Page 2 of 2 FirstFirst 12
Results 21 to 31 of 31
Like Tree4Likes

Thread: is this the right way?

  1. #21
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: is this the right way?

    Quote Originally Posted by lanmonster View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  2. #22
    lanmonster is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default 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 02:22 AM.

  3. #23
    faustnero is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default 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. #24
    lanmonster is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default 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. #25
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: is this the right way?

    Quote Originally Posted by faustnero View Post
    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...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #26
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default Re: is this the right way?

    Quote Originally Posted by lanmonster View Post
    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;
    lanmonster likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #27
    lanmonster is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default Re: is this the right way?

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

  8. #28
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #29
    lanmonster is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default 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. #30
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: is this the right way?

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

    db
    lanmonster likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #31
    lanmonster is offline Member
    Join Date
    Dec 2012
    Posts
    22
    Rep Power
    0

    Default 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 04:21 PM.

Page 2 of 2 FirstFirst 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
  •