Results 1 to 12 of 12
  1. #1
    louiedogg418 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default 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. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    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. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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. #4
    louiedogg418 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default

    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

  5. #5
    louiedogg418 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default

    opps the code is for(int month=1 ;month<=(yearstot*12);month++) // this is the code that is throwing operator exception

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

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

  7. #7
    louiedogg418 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default

    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. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    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. #9
    louiedogg418 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default

    now to figure out how to get the calculations to work, I will probably be back in a little bit. :)

  10. #10
    louiedogg418 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default

    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. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    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. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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!

Similar Threads

  1. A bit of confusion with passing variables/infinite loop
    By kaizen9001 in forum New To Java
    Replies: 12
    Last Post: 03-27-2011, 10:05 PM
  2. creating variables in a for loop?
    By sehudson in forum New To Java
    Replies: 7
    Last Post: 03-01-2011, 04:37 AM
  3. Replies: 2
    Last Post: 01-13-2011, 06:43 AM
  4. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 06:48 PM
  5. Trouble with For loop and variables in a program
    By dablyz in forum New To Java
    Replies: 12
    Last Post: 05-06-2008, 05:25 AM

Posting Permissions

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