# Thread: Using variables in a loop

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

## Using variables in a loop

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

public class MortgageApp2 {
public static void main(String args[])
{
String loanamt,
interest_peryr,
yearstot,
pay_amt;

double loan,
interest_yr,
years,
interest_mo,
loan_pmts,
payments,
new_balance,
old_balance,
interest,
principle,
payamt,
balance;

loanamt= JOptionPane.showInputDialog("Enter Loan Amount");
interest_peryr=JOptionPane.showInputDialog("Enter the Interest Percentage: Example 5.6");
yearstot=JOptionPane.showInputDialog("Enter Loan Period in Years");

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;

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

JOptionPane.showMessageDialog (null, "Your payment is: \$ " + payments);

principle= payments - (loan *interest_mo);

JOptionPane.showMessageDialog(null, "your prinicple paid is: \$ " + principle);

interest= payments-principle ;
JOptionPane.showMessageDialog(null, "your interest paid is: \$ " + interest);

for(int month=yearstot ;month<=(yearstot*12);month++){

}

System.exit(0);

}
}```

So in the last line of my code i am trying to call the years of the loan entered by the user to do a loop in months, so it would be year * 12 so i can get the total amount of months to increment the loop, and then from there i want to add the calculation to do principle and so on, but how do i get the loops to run bu using user defined years?
Thanks!!

2. how do i get the loops to run bu using user defined years
Are you talking about the yearstot variable?
Your code shows that its value is being read in from the user.

Can you explain what is wrong with your current code? It appears that the loop is controlled by a value entered by the user.

What values do you want for the month variable inside of the loop?

3. Please make an effort to indent your code consistently. This will make it easier to read. Not only for us but also for you. As a learner sometimes it is difficult to see things when the code is all over the place like a mad womans knitting.

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

Ok so i made a mistake in the code, it is now as follows :

for(int month=yearstot ;month<=(yearstot*12);month++)

So i need the loop to start at 1(the first month payment) and end at yearstot*12 (the last month payment)
So that each month it will increment until the loops hits the last month of the mortgage, from there will put im all the calculations.
Right now i am getting a operator cannot be applied error with the above code :/ not to good with loops yet

5. Member
Join Date
Jun 2011
Posts
52
Rep Power
0
opps the code is for(int month=1 ;month<=(yearstot*12);month++) // this is the code that is throwing operator exception

6. yearstot is a String. Please tell me what the result of "hello" * 2 will be.

7. Member
Join Date
Jun 2011
Posts
52
Rep Power
0
oooooooo!! so if i change it to years it worked!! ahahah! i been going at this all day, starting to miss things! Thank you!

8. code that is throwing operator exception
When you get an error, please copy and paste the full text of the error here.

What data type is yearstot? Can you multiply it by 12?

Look at the Integer class's methods if you need to convert to a integer.

9. Member
Join Date
Jun 2011
Posts
52
Rep Power
0
now to figure out how to get the calculations to work, I will probably be back in a little bit. :)

10. Member
Join Date
Jun 2011
Posts
52
Rep Power
0
Java Code:
```import javax.swing.*;

public class MortgageApp2 {
public static void main(String args[])
{
String loanamt,
interest_peryr,
yearstot,
pay_amt;

double loan,
interest_yr,
years,
interest_mo,
loan_pmts,
payments,
new_balance,
old_balance,
interest,
principle,
payamt,
balance;

loanamt= JOptionPane.showInputDialog("Enter Loan Amount");
interest_peryr=JOptionPane.showInputDialog("Enter the Interest Percentage: Example 5.6");
yearstot=JOptionPane.showInputDialog("Enter Loan Period in Years");

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;

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

JOptionPane.showMessageDialog (null, "Your payment is: \$ " + payments);

principle= payments - (loan *interest_mo);

JOptionPane.showMessageDialog(null, "your prinicple paid is: \$ " + principle);

interest= payments-principle ;
JOptionPane.showMessageDialog(null, "your interest paid is: \$ " + interest);

for(int month=1 ;month<=(years*12);month++){
balance= balance -(loan-principle);
//new_balance=bala

System.out.println("balance is " + balance);

}

System.exit(0);

}
}```

ok so now that i got the loop to work.... Now, but all it is doing is repeating the same thing over and over again. I am new to mortgages and loops, still in college, and 2nd week of my first java class. How do i get the prgram to take a payment off each time the loop goes threw a month?

So the first one takes the principle off the loan, but then how to i get the program to take take the new balance into account and subtract another payment?

11. how to i get the program to take take the new balance into account and subtract another payment?
Write down the steps needed to do the processing. How would you do it with paper and pencil?
You must understand the algorithm before you try to code it.

12. Your problem now is a Math one and nothing to do with Java. You need to step away from the computer, grab a pen and some paper and work out how to calculate your sums. Once you have got it clear in your head (and on paper) you can then easily write the code to do what you want.

SNAP!

#### Posting Permissions

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