# Program for retirement funds..(STUCK)

Shawn Houston
Greenhorn
Posts: 22
Ok, I'm trying to write a program that will prompt the user to enter the amount of money they have saved for their retirement. Now, saying that the user deducts 2000.00 a month from the account and recieves 5% interest over thier retirement years. I want to prompt the user on how many years their retirement funds will last them. Here is what i have for code.

import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class retire
{

public static void main(String[] args) throws IOException
{
double saved;
double interest;
double total;
double balance;
double years = 1;
String Outputstr1;

Outputstr1 = JOptionPane.showInputDialog("Enter how much money you have saved for retirement.");
saved = Double.parseDouble(Outputstr1);

total = saved - (12 * 2000.00);
interest = total * .05;
balance = total + interest;

while(balance >= 0)
{
JOptionPane.showMessageDialog(null,"Your retirement fund will last " + years + " years","FUNDS",JOptionPane.INFORMATION_MESSAGE);
}
System.exit(0);
}
}

Now, I think my problem has to do with my declaration of years, and of course my while loop. I'm having problems grasping the while loop, Very new to the concept. Anyone have any suggestions on what i should do?

Mike Gershman
Ranch Hand
Posts: 1272
Since you rightly didn't ask us to just fix your code, I won't.

Your program should have three parts. The first part inputs the starting balance.

The middle part should be the while loop and repeatedly reduces the balance by a year's payments but increases the balance by a year's interest. This continues untli the balance reaches zero.

The last part outputs the number of years it took to reach zero. (Don't forget to count them in your loop!)

Give this a try and come back to us if you have more questions.
[ October 16, 2004: Message edited by: Mike Gershman ]

Shawn Houston
Greenhorn
Posts: 22
Ok, after looking and relooking at the code. I can't quite figure out where to put the code to take the balance + interest from the amount saved repeatedly till it reaches 0. Years I believe is supposed to be :
Years = 1;
Then in my while loop i would count it as years = years + 1?
This is where im Stuck because I can't figure hte code to reduce the amount saved my loop goes into an infinite loop.

import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class retire
{

public static void main(String[] args) throws IOException
{
double saved;
double interest;
double total;
double balance;
double years = 0;
double funds;
String Outputstr1;

Outputstr1 = JOptionPane.showInputDialog("Enter how much money you have saved for retirement.");
saved = Double.parseDouble(Outputstr1);

total = saved - (12 * 2000.00);
interest = total * .05; //Should any of this be within my while
balance = total + interest; //loop? I'm somewhat thinking this might
funds = saved - balance; //be to many calculations but,
//sure.
while(funds != 0)
{
years = years + 1; //(code for reducing the amount saved should be in
//here but i cant quite figure it out)
}
JOptionPane.showMessageDialog(null,"Your retirement fund will last " + years + " years","FUNDS",JOptionPane.INFORMATION_MESSAGE);
}

}

Shawn Houston
Greenhorn
Posts: 22
Ok i got the program to run. The problem I'm running into now is when I enter an amount say 100,000.00 it returns it will last me 11 years. Now the higher the amount i put in , say 480,000.00 says it will only last me 3 years. My calculations dont seem to be backwards from what i can see..maybe someone else can see something wrong here?

import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class retire
{

public static void main(String[] args) throws IOException
{
double saved;
double interest;
double total;
double balance, newbalance = 0;
double years = 1;
double funds;
String Outputstr1;

Outputstr1 = JOptionPane.showInputDialog("Enter how much money you have saved for retirement.");
saved = Double.parseDouble(Outputstr1);

balance = saved - 24000.00;
interest = balance * .05;
total = balance + interest;
newbalance = saved - total;

while(newbalance >= 0)
{
balance = newbalance - 24000.00;
interest = balance * .05;
total = balance + interest;
newbalance = saved - total;

years = years + 1;
}
JOptionPane.showMessageDialog(null,"Your retirement fund will last " + years + " years","FUNDS",JOptionPane.INFORMATION_MESSAGE);
}
}

Layne Lund
Ranch Hand
Posts: 3061
I don't see any obvious problems with your code. However, I have a suggestion to help you figure out what is happening. First, make a table with the name of each variable across the top:

Then step through each line of code and fill in the values assigned to each variable. Perhaps putting the above table next to your code would help:

And so on. When you get to the loop, you should probably copy the code that is repeated several times so you can show the modified the values in the table easily.

Tracing through a program in this fashion will help you catch any logic errors you may have made. If you don't find any, you should then add System.out.println() statements to output the values of the variables and verify that they match what you calculated in the above table. If you find a problem, it should be a little easier to track down the cause.

HTH

Layne
[ October 16, 2004: Message edited by: Layne Lund ]

Shawn Houston
Greenhorn
Posts: 22
Ok, I did what you said and traced it out. It seems I was taking the newbalance - the newbalance basically leaving the user with , If they entered 100000.00 they had saved after the first year they only had 15200.00 left in the account so I removed newbalance totally. Now when I did that I went back to infinite loop in my code. So I'm currently plugging away at trying to fix this loop.

import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class retire
{

public static void main(String[] args) throws IOException
{
double saved;
double interest;
double total;
double balance, newbalance;
double years = 1;
double funds;
String Outputstr1;

Outputstr1 = JOptionPane.showInputDialog("Enter how much money you have saved for retirement.");
saved = Double.parseDouble(Outputstr1);

balance = saved - 24000.00;
interest = balance * .05;
total = balance + interest;

while(total >= 0)
{
balance = saved - 24000.00;
System.out.println(balance);

interest = balance * .05;
System.out.println(interest);

total = balance + interest;
System.out.println(total);

years = years + 1;
}
JOptionPane.showMessageDialog(null,"Your retirement fund will last " + years + " years","FUNDS",JOptionPane.INFORMATION_MESSAGE);
}
}

Now where I'm stuck Is after the total statment within the while loop. After that The program needs to deduct another 24000.00 until the total = 0. So does the total statement in the whileloop need to be set to another variable? I'm so confused atm and the book I have is of absolute no help.
[ October 16, 2004: Message edited by: Shawn Houston ]

Layne Lund
Ranch Hand
Posts: 3061
You might want to trace the code again to see why you are getting an infinite loop.

Layne

Layne Lund
Ranch Hand
Posts: 3061

Can be replaced with

or with the shortcut

Similarly, you can deduct the "withdrawal" directly from balance:

As a side note, there is also a shortcut for

Namely, you can replace the above line with

HTH

Layne

p.s. Please use UBB CODE tags when posting code. It will help preserve your formatting so we can read it more easily. There are buttons just below the edit box where you type your message.

Thanks :-)
[ October 16, 2004: Message edited by: Layne Lund ]

Shawn Houston
Greenhorn
Posts: 22
That's the part that I'm totally confused on. I can't see why I would be getting an infinite loop. I'm missing something critical somewhere. The program seems to be deducting the funds, adding the interest and creating the new total fine. BUT, It just keeps doing the occurance for the first year and thats it. It doesnt deduct another 240000.00 , add the interest and create another new total, until the account has a balance of 0 or the first negative value that is created.

Shawn Houston
Greenhorn
Posts: 22
Ok, I figured it out. What was going on was I wasn't subtracting the balance from the newbalance. Oh and thanks for pointing out the UBB for the code I was wondering how people posted code like that. You've been very helpful and can't thank you enough.

I had to switch the (saved) with balance in the first statement within the while loop.

Layne Lund
Ranch Hand
Posts: 3061
Originally posted by Shawn Houston:
Ok, I figured it out. What was going on was I wasn't subtracting the balance from the newbalance. Oh and thanks for pointing out the UBB for the code I was wondering how people posted code like that. You've been very helpful and can't thank you enough.

I had to switch the (saved) with balance in the first statement within the while loop.

Yup, you got it! So with the modifications, your code probably looks something like this:

By using the shortcuts I described above, you can change it to the shorter form:

You can also remove some redundant code by subtracting the 24,000 dollars at the end of the loop:

Good job on finding a working solution!

Keep coding!

Layne