Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Help Calculating The Balance Of 3 Variables  RSS feed

 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would i assign a value to a variable? Like this?

double deposit = 0D;
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly.
Be careful. I noticed that you confuse your interest variable (which is supposed to hold amount of interest gained) with interestRate (which is supposed to hold how much percent you gain per year).
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i did it and it works now but i don't think i'm getting the right values. This is what it produced,

Enter your initial deposit amount in dollars

1500
Enter the annual interest rate as a percentage (eg. 6.0)

6.0
Enter your monthly deposit amount in dollars

500
Savings growth over the next 6 months:

Balance after first month: $1500.0
Interest earned for this month: $6.0
Balance after second month: $0.0
Interest earned for this month: $0.0
Balance after third month: $0.0
Interest earned for this month:

Balance after fourth month: $500.0
Interest earned for this month:

Balance after fifth month: $1000.0
Interest earned for this month:

Balance after sixth month: $1500.0
Interest earned for this month:
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also here is the code,

 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't check it now. My suggestion is you "execute" your program using pencil and paper. Write down values of all variables and note if any value change. You should find the place when you make your mistake. And it would be helpful if you pasted your full code again.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure thing i will and here is the full code

 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You print out the balance after the first month on line 91, but you don't actually calculate the balance until line 92 (where you add the deposit) and lines 94 and 95 (where you calculate and add the interest). You need to reorder those lines.
For the second month you do the same.
For the remainder of the months, you do something similar, but you have suddenly decided to stop adding interest.

Also note that you are still mixing up your interest and interestRate variables.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Added the rest of the lines but it's still not working properly heres my code now,



What else do i need to add? Are you able to show me?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:What else do i need to add? Are you able to show me?

Did you read the first paragraph of my last post ?
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:
Johnnoo Williams wrote:What else do i need to add? Are you able to show me?

Did you read the first paragraph of my last post ?


I did but i don't understand what i need to change? And how to change it?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:
Stuart A. Burkett wrote:
Johnnoo Williams wrote:What else do i need to add? Are you able to show me?

Did you read the first paragraph of my last post ?


I did but i don't understand what i need to change? And how to change it?

Have a look at lines 23 - 27. Java executes lines in sequence, so it will execute line 23, then line 24, then line 25, then line 26 and then line 27. Do you think that's the right order for those lines to be executed ? If not, then just rearrange them.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did and it worked a bit better this time but only the first month,



Enter your initial deposit amount in dollars

1500
Enter the annual interest rate as a percentage (eg. 6.0)

6.0
Enter your monthly deposit amount in dollars

500
Savings growth over the next 6 months:

Balance after first month: $1500.0
Interest earned for this month: $6.0
Balance after second month: $0.0
Interest earned for this month: $0.0
Balance after third month: $0.0
Interest earned for this month: $0.0
Balance after fourth month: $0.0
Interest earned for this month: $0.0
Balance after fifth month: $0.0
Interest earned for this month: $0.0
Balance after sixth month: $0.0
Interest earned for this month: $0.0
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:I did and it worked a bit better this time but only the first month,

That code seems to be identical to the last lot of code you posted.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I reordered the double variables up the top like you said?
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that's not it what order should i have it? Are you able to show me cause i'm really unsure.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:I reordered the double variables up the top like you said?

I said nothing about the variables order. Look at the line numbers I was referring to.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean these ones?

System.out.println ("Balance after first month: $" + deposit);
deposit = deposit + monthly;
System.out.println ("Interest earned for this month: $" + interest);
interest = balanceCurrent * ((interestRate / 12) / 100);
deposit = balanceCurrent + interest;
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:Do you mean these ones?

Yes
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the correct order? I tried it this way but didn't work.

deposit = deposit + monthly;
interest = balanceCurrent * ((interestRate / 12) / 100);
deposit = balanceCurrent + interest;
System.out.println ("Balance after first month: $" + deposit);
System.out.println ("Interest earned for this month: $" + interest);
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's better. I suspect your problem now is that on line 16 you read in the interest rate and assign it to the interest variable. I think that should be the interestRate variable.

Also I don't think you need the balanceCurrent variable. That seems to be the same thing as your deposit variable, but you are using them interchangeably. I suspect if you replace balanceCurrent with deposit, you should be pretty close.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still doesn't work i'm getting no values now.

Are you able to show me how you would code that part? So i can see what exactly i'm doing wrong?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:Still doesn't work i'm getting no values now.

I edited my previous post before I saw this. See the second part that I added.

And then post your latest code (just the InvestmentReport method) and the output if it still doesn't work.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still isn't displaying the correct values,



It should be printing out this,

Enter your initial deposit amount in dollars: 1000
Enter the annual interest rate as a percentage (eg. 6.0): 6.0
Enter your monthly deposit amount in dollars: 500
Savings growth over the next 6 months:
Balance after first month: $1507.50
Interest earned for this month: $7.50
Balance after second month: $2017.54
Interest earned for this month: $10.04
Balance after third month: $2530.13
Interest earned for this month: $12.59
Balance after fourth month: $3045.28
Interest earned for this month: $15.15
Balance after fifth month: $3563.00
Interest earned for this month: $17.73
Balance after sixth month: $4083.32
Interest earned for this month: $20.32

But it's printing out this,

Enter your initial deposit amount in dollars

1500
Enter the annual interest rate as a percentage (eg. 6.0)

6.0
Enter your monthly deposit amount in dollars

500
Savings growth over the next 6 months:

Balance after first month: $2010.0
Interest earned for this month: $10.0
Balance after second month: $2522.55
Interest earned for this month: $12.55
Balance after third month: $3037.66275
Interest earned for this month: $15.112750000000004
Balance after fourth month: $3555.35106375
Interest earned for this month: $17.68831375
Balance after fifth month: $4075.62781906875
Interest earned for this month: $20.27675531875
Balance after sixth month: $4598.5059581640935
Interest earned for this month: $22.878139095343748
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the initial deposit in the 'correct' output ? What's the initial deposit in the 'wrong' output ?
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Initial Deposit in both correct and wrong is $1000

It looks like the numbers need to be rounded off to the closest decimal number? Do you know how to do that?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:Initial Deposit in both correct and wrong is $1000

Are you sure. Looks like 1500 in the wrong one to me.
Johnnoo Williams wrote:It looks like the numbers need to be rounded off to the closest decimal number? Do you know how to do that?

Have a look at the DecimalFormat class.
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh sorry yeah your right 1500 is the wrong one.

And thanks i'll take a look at that link
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So would i have to use concat to bring in the decimal places? I'm still unsure about how to do it?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:So would i have to use concat to bring in the decimal places? I'm still unsure about how to do it?

Impressive that you managed to read and take in everything in that link in less than 3 minutes. Maybe this tutorial will help.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm surprised nobody has mentioned this before, but generally it is a bad idea to store money using a floating point type. Money comes in discrete units. In the U.S., that unit is pennies. So instead of storing 100.00 dollars, you should be storing 10,000 pennies. if the user inputs dollars, you should do the conversion.

Then, when you go to display it, you can format it however you like.

just something to consider...
 
Johnnoo Williams
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Took a little read of that but still don't fully understand it. Are you able to show me how it's done so i can learn from that?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnnoo Williams wrote:Took a little read of that but still don't fully understand it. Are you able to show me how it's done so i can learn from that?

There are examples in the tutorial. You'll learn far more by copying and playing around with those examples than you will from me simply giving you the answer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!