1. Member
Join Date
Jun 2011
Posts
52
Rep Power
0

## About to pull my hair out!!

I still cant get my loop to work, it goes the amount of months but it wont recalculate each iteration.... I am so frustrated , I have changed things around numerous times and still have not got any where!! if any one can help please it would be appreciated!

Java Code:
```import javax.swing.*;

public class MortgageApp2 {
public static void main(String args[])
{

double loan,
interest_yr,
years,
interest_mo,
loan_pmts,
payments,
new_balance,
interest,
interestamt,
principal,
principalamt,
payamt,
balance,
loanbal;

//Above is all the varaibles used for calculations

String loanamt,
interest_peryr,
yearstot,
pay_amt;
//The above declares the strings for the program

loanamt= JOptionPane.showInputDialog("Enter Loan Amount");    //Obtains input from user for loan amount
interest_peryr=JOptionPane.showInputDialog("Enter the Interest Percentage: Example 5.6");  //Obtains input from user for interest %
yearstot=JOptionPane.showInputDialog("Enter Loan Period in Years");  //Obtains input for length of loan

// Below is the code to parse stings into doubles
loan=Double.parseDouble(loanamt);
interest_yr=Double.parseDouble(interest_peryr);
years=Double.parseDouble(yearstot);

interest_mo = interest_yr/12/100; // Turns percent whole numbers into decimals

loan_pmts= years * 12;  //Gets total amount of months the loan will last after user inputs length of loan in years

payments = (loan*interest_mo/(1- Math.pow((1+interest_mo),-loan_pmts))); //Loan calculation

JOptionPane.showMessageDialog (null, String.format( "Your payment is: \$ %.2f ", + payments)); //Shows payment with 2 decimal places

principalamt= payments - (loan *interest_mo);  //calculates principle

JOptionPane.showMessageDialog(null, String.format( "your prinicple paid is: \$ %.2f ", + principalamt)); //shows principle with 2 decimal places

interestamt= payments-principalamt ;  //calculates interest

JOptionPane.showMessageDialog(null, String.format( "your interest paid is: \$ %.2f ", + interestamt)); //shows interest with 2 decimal places

//below is the loop

for(double month=1;month<=(years*12);month++)
{interest=loan*interest_mo;
principal=payments-interest;
balance=loan-principal;
System.out.println("you balance is" + balance);
}

System.exit(0);```

2. but it wont recalculate each iteration..
I assume you mean it outputs the same value everytime thru the loop.
If you want a value to change from one time thru the loop to another you need to save the new value that is computed in the loop and use it as the starting value for the next time thru the loop.

Java Code:
```var = start_value
loop
.. compute change
var = var - change; // change var to new value
// now var has been changed
end loop```

3. Member
Join Date
Jun 2011
Posts
52
Rep Power
0
Java Code:
```interest=loan*interest_mo;
principal=payments-interest;
balance=loan-principal;

for(double month=1;month<=(years*12);month++)
{balance=loan-principal;
balance=balance-principal;
System.out.println("you balance is" + balance);
}```
did this but still have no change :(

4. Please post the output you get that shows the problem.
Add some more printlns to the code to show the values of ALL the variables used in the loop. Put one after Each line to show the value of the variables used on the previous line. The output will show you what is wrong.

Be sure to add id Strings to identify the variables:
System.out.println("balance=" + balance + ", add the other variables ...);

5. Member
Join Date
Jun 2011
Posts
52
Rep Power
0
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453
you balance is199562.1773279453

Process completed.

I just get that same thing for the amount of what ever years i input.... so for a 30 year loan it repeats about 360 times

6. Now do what I recommended:

Add some more printlns to the code to show the values of ALL the variables used in the loop. Put one after Each line to show the value of the variables used on the previous line. The output will show you what is wrong.

Be sure to add id Strings to identify the variables:
System.out.println("balance=" + balance + ", add the other variables ...);

Don't post all of the output, just that for the first 4 months should be enough.

7. Member
Join Date
Jun 2011
Posts
52
Rep Power
0
Ok i am getting a little bit closer, i still cant get the loop to repeat the new_balance with out coding it.
here is my new code.

Java Code:
```import javax.swing.*;

public class MortgageApp2 {
public static void main(String args[])
{

double loan,
interest_yr,
years,
interest_mo,
loan_pmts,
payments,
new_balance,
interest,
interestamt,
principal,
principalamt,
payamt,
balance,
loanbal,
new_interest;

//Above is all the varaibles used for calculations

String loanamt,
interest_peryr,
yearstot,
pay_amt;
//The above declares the strings for the program

loanamt= JOptionPane.showInputDialog("Enter Loan Amount");    //Obtains input from user for loan amount
interest_peryr=JOptionPane.showInputDialog("Enter the Interest Percentage: Example 5.6");  //Obtains input from user for interest %
yearstot=JOptionPane.showInputDialog("Enter Loan Period in Years");  //Obtains input for length of loan

// Below is the code to parse stings into doubles
loan=Double.parseDouble(loanamt);
interest_yr=Double.parseDouble(interest_peryr);
years=Double.parseDouble(yearstot);

interest_mo = interest_yr/12.0/100.0; // Turns percent whole numbers into decimals

loan_pmts= years * 12;  //Gets total amount of months the loan will last after user inputs length of loan in years

payments = (loan*interest_mo/(1- Math.pow((1+interest_mo),-loan_pmts))); //Loan calculation

JOptionPane.showMessageDialog (null, String.format( "Your payment is: \$ %.2f ", + payments)); //Shows payment with 2 decimal places

principalamt= payments - (loan *interest_mo);  //calculates principle

JOptionPane.showMessageDialog(null, String.format( "your prinicple paid is: \$ %.2f ", + principalamt)); //shows principle with 2 decimal places

interestamt= payments-principalamt ;  //calculates interest

JOptionPane.showMessageDialog(null, String.format( "your interest paid is: \$ %.2f ", + interestamt)); //shows interest with 2 decimal places

//below is the loop

for(double month=1;month<=(years*12);month++)
{interest=loan*interest_mo;
System.out.println("interest "+interest);

principal=payments-interest;
System.out.println("principal "+ principal);

balance=loan-principal;
System.out.println("balance "+ balance);

new_interest=balance*interest_mo;
System.out.println("new_interest"+new_interest);

new_balance=balance-(payments-new_interest);
System.out.println("new_balance= " + new_balance);

new_balance=new_balance-principal;
System.out.println("new_balance=" + new_balance);

}

System.exit(0);

}
}```

and here is the output for a couple months:

interest 958.3333333333335
principal 208.8123795537649
balance 199791.18762044623
new_interest957.3327740146383
new_balance= 199581.37468157377
new_balance=199372.56230202
interest 958.3333333333335
principal 208.8123795537649
balance 199791.18762044623
new_interest957.3327740146383
new_balance= 199581.37468157377
new_balance=199372.56230202
interest 958.3333333333335
principal 208.8123795537649
balance 199791.18762044623
new_interest957.3327740146383
new_balance= 199581.37468157377
new_balance=199372.56230202
interest 958.3333333333335
principal 208.8123795537649
balance 199791.18762044623
new_interest957.3327740146383
new_balance= 199581.37468157377
new_balance=199372.56230202

So i cant get the new_balance to keep calculating? I did figure out to how to recalculate interest depending on the new balance tho, but how do i repeat this?!

8. Member
Join Date
Jun 2011
Posts
52
Rep Power
0
Also what did you mean by using ID Strings? how would that look?

9. Member
Join Date
Jun 2011
Posts
52
Rep Power
0
Or is there a way to set the conditions of the loop until the balance is = 0 using the same calculations i have above? I am so lost on this...

10. You've done it. All the numbers you printed have a String id in before them. One problem with the labels is that it appears that you used the same String with more than one println: new_balance. You should make all id Strings unique so you know which println printed them. Add a digit to it. For example: 1new_balance

In looking at what you've printed do you see that new_balance is always the same value. Shouldn't it decrease each loop? Where in your code do you compute that value that it is always the same and not decreasing?

11. Are the values that print out correct?

12. Member
Join Date
Jun 2011
Posts
52
Rep Power
0
yea the values are correct, all the numbers seem to line up with a chart im using off the internet with the same int, year, and loan amounts... I just cant seem too see where my code computes the same value....this thing is eating my soul...

ok here is what i just changed...

Java Code:
```  for(double month=1;month<=(years*12);month++)
{interest=loan*interest_mo;
System.out.println("interest "+interest);

principal=payments-interest;
System.out.println("principal "+ principal);

balance=loan-principal;
System.out.println("balance "+ balance);

new_interest=balance*interest_mo;
System.out.println("new_interest"+new_interest);

new_principal=payments-new_interest;
System.out.println("new pricipal " + new_principal);

new_balance=balance-new_principal;
System.out.println("new_balance " +new_balance);

new_balance2=new_balance-new_principal;
System.out.println("new_balance2 " +new_balance2);```

13. Member
Join Date
Jun 2011
Posts
52
Rep Power
0
still getting this output:

interest 958.3333333333335
principal 208.8123795537649
balance 199791.18762044623
new_interest957.3327740146383
new pricipal 209.8129388724601
new_balance 199581.37468157377
new_balance2 199371.5617427013
interest 958.3333333333335
principal 208.8123795537649
balance 199791.18762044623
new_interest957.3327740146383
new pricipal 209.8129388724601
new_balance 199581.37468157377
new_balance2 199371.5617427013

Does it have something to do with my actual original balance??

14. I assume the balance should be continually decreasing. The output shows that it is being reset to its original value. Check all assignment statements that put values into the balance. One of them is not using the old value. See the logic in post#2

balance 199791.18762044623 <<<<<<<<<< Original

new_interest957.3327740146383
new pricipal 209.8129388724601
new_balance 199581.37468157377
new_balance2 199371.5617427013 <<<< OK this is less. You'd expect the balance to go down

interest 958.3333333333335
principal 208.8123795537649
balance 199791.18762044623 <<<???? what happened here? Its back to the original

new_interest957.3327740146383
new pricipal 209.8129388724601
new_balance 199581.37468157377 <<<<<<<< These are a repeat ???
new_balance2 199371.5617427013

15. Member
Join Date
Jun 2011
Posts
52
Rep Power
0
hmmmm..... so how do i get the balance to go down after i calculate the new_balance?

16. See post#2

17. Member
Join Date
Jun 2011
Posts
52
Rep Power
0
Java Code:
``` for(double month=1;month<=(12);month++)
{interest=loan*interest_mo;
System.out.println("interest "+interest);

principal=payments-interest;
System.out.println("principal "+ principal);

balance=loan;
System.out.println("balance "+ balance);

balance=balance-principal;
System.out.println("balance2 "+balance);

new_interest=balance*interest_mo;
System.out.println("new_interest"+new_interest);

new_principal=payments-new_interest;
System.out.println("new pricipal " + new_principal);

new_balance=balance-new_principal;
System.out.println("new_balance " +new_balance);

new_balance=new_balance-new_principal;
System.out.println("new new balance is " + new_balance);```
still didnt change any thing :(

18. Member
Join Date
Jun 2011
Posts
52
Rep Power
0
interest 958.3333333333335
principal 208.8123795537649
balance 200000.0
balance2 199791.18762044623
new_interest957.3327740146383
new pricipal 209.8129388724601
new_balance 199581.37468157377
new new balance is 199371.5617427013

19. Does the print out show that the balance is decreasing each loop?

20. Member
Join Date
Jun 2011
Posts
52
Rep Power
0
no, it stays the same.

Java Code:
```for(double month=1;month<=(12);month++)
{interest=loan*interest_mo;
System.out.println("interest "+interest);

principal=payments-interest;
System.out.println("principal "+ principal);

balance=loan;
System.out.println("balance "+ balance);

balance=balance-principal;
System.out.println("balance2 "+balance);

new_balance=balance-principal;
System.out.println("new_balance " +new_balance);

new_balance=new_balance-principal;
System.out.println("new new balance is " + new_balance);```
Returns

principal 208.8123795537649
balance 200000.0
balance2 199791.18762044623
new_balance 199582.37524089246
new new balance is 199373.5628613387
interest 958.3333333333335
principal 208.8123795537649
balance 200000.0
balance2 199791.18762044623
new_balance 199582.37524089246
new new balance is 199373.5628613387

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•