Results 1 to 12 of 12
Thread: Using variables in a loop
- 06-28-2011, 03:05 AM #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!!
- 06-28-2011, 03:10 AM #2
Are you talking about the yearstot variable?how do i get the loops to run bu using user defined years
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?
- 06-28-2011, 03:13 AM #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.
- 06-28-2011, 03:25 AM #4
Member
- Join Date
- Jun 2011
- Posts
- 52
- Rep Power
- 0
sorry about the indentation.
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
- 06-28-2011, 03:26 AM #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
- 06-28-2011, 03:28 AM #6
yearstot is a String. Please tell me what the result of "hello" * 2 will be.
- 06-28-2011, 03:30 AM #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!
- 06-28-2011, 03:30 AM #8
When you get an error, please copy and paste the full text of the error here.code that is throwing operator exception
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.
- 06-28-2011, 03:30 AM #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. :)
- 06-28-2011, 03:47 AM #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?
- 06-28-2011, 03:50 AM #11
Write down the steps needed to do the processing. How would you do it with paper and pencil?how to i get the program to take take the new balance into account and subtract another payment?
You must understand the algorithm before you try to code it.
- 06-28-2011, 03:52 AM #12
Similar Threads
-
A bit of confusion with passing variables/infinite loop
By kaizen9001 in forum New To JavaReplies: 12Last Post: 03-27-2011, 09:05 PM -
creating variables in a for loop?
By sehudson in forum New To JavaReplies: 7Last Post: 03-01-2011, 03:37 AM -
Making a loop and creating variables based off of user input
By seanfmglobal in forum New To JavaReplies: 2Last Post: 01-13-2011, 05:43 AM -
What are Instance variables and static variables?
By sandeshforu in forum New To JavaReplies: 3Last Post: 09-09-2009, 05:48 PM -
Trouble with For loop and variables in a program
By dablyz in forum New To JavaReplies: 12Last Post: 05-06-2008, 04:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks