1

I'm building a project for school and I want to make three money buttons (quarter, nickel and dime) that accumulates their value with every click of the button. When I'm done the text displayed will be grabbed and reParsed back to a double. I have that part done, I'm just drawing a blank on using a button accumulator.

     double quarter = 0.0;

     if (event.getSource() == quarterButton)
     {  
        Possible loop???
        quarter += .25;
     }
     String quarter2 = Double.toString(quarter);
     amountDeposited.setText(quarter2);  

It doesn't work... It just prints .25 and stops. I need it to accumulate each time the button is pressed. I can change the double to anything really that was just what I had in there now...

Shawn
  • 9
  • 2
  • 6
  • http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – Jack Jan 30 '15 at 02:40
  • 2
    What's your actual question? – Alex K Jan 30 '15 at 02:43
  • What makes you think you need a loop? Loops are for doing things repeatedly, but in your case, you (presumably) only want to add one quarter to your quarter field each time the user presses a button. You'd only add a loop there if you wanted X quarters to be added or something like that. – MarsAtomic Jan 30 '15 at 02:57
  • At this point I'm just grabbing at anything... I've done accumulators before, but only in loops so far... – Shawn Jan 30 '15 at 03:47
  • 1
    Is your `quarter` variable declared at the class level or inside the method you are showing? If it's in the method then that might explain your issue: it's being reset to 0 every time the button is clicked. – sprinter Jan 30 '15 at 04:03
  • Answered by Sprinter – Shawn Jan 30 '15 at 18:45

2 Answers2

1

I suspect your issue is that your quarter variable is declared inside the method you are showing. If so then it will be reset to 0 at each click. It needs to be declared at the class level so that it maintains its value between button clicks.

sprinter
  • 27,148
  • 6
  • 47
  • 78
0

Do not use double for Money values as not all floating point values can be exactly represented by a Java double. Consider using BigDecimal.

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • 1
    This is a good point, but does it answer his question? – MarsAtomic Jan 30 '15 at 02:59
  • That is a good question in itself ;-) I have mind reading powers and when it the OP does finally ask a question, this will be the ideal answer ;-) (or I will delete it with haste) – Scary Wombat Jan 30 '15 at 03:02
  • 1
    I'm betting the question is something else, because he posted code that looks like it should work -- I think the issue is that he didn't define his requirements well in the first place and has confused himself. This said, his next question will require your answer, for sure. – MarsAtomic Jan 30 '15 at 03:06
  • You win the bet, here's your $0.2500000001 – Scary Wombat Jan 30 '15 at 04:11
  • `String.format("You win the bet, here's your $%.2f", doubleMoney);`. It is not always evil to use floating point for money if you know what you are doing and how it should be done. Without proper knowledge, using `BigDecimal` can be as evil as floating point. I have seen people initializing a `BigDecimal` with a `double` without setting precision, doing division without providing precision and rounding method, and even get the `doubleValue` out from the `BigDecimal` for comparison – Adrian Shum Jan 30 '15 at 04:21
  • My goodness, who on earth upvoted this answer? – Scary Wombat Jan 30 '15 at 04:27
  • @AdrianShum Very good points. – Scary Wombat Jan 30 '15 at 04:28