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

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

    Default

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

    Default

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

    Default

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

    Default

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

    Default

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

    Default

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

    Default

    Also what did you mean by using ID Strings? how would that look?

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

    Default

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

    Default

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

    Default

    Are the values that print out correct?

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

    Default

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

    Default

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

    Default

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

    Default

    hmmmm..... so how do i get the balance to go down after i calculate the new_balance?

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    See post#2

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

    Default

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

    Default

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

    Default

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

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

    Default

    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 LastLast

Similar Threads

  1. About to pull my hair out. :(
    By cfox10 in forum New To Java
    Replies: 8
    Last Post: 04-05-2011, 06:03 AM
  2. Junit. Pulling my HAIR OUT!
    By phil128 in forum Eclipse
    Replies: 2
    Last Post: 10-04-2010, 08:06 PM
  3. Replies: 0
    Last Post: 11-15-2008, 08:29 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
  •