Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Trouble with my loop ~I think

Trouble with my loop ~I think

Scheduled Pinned Locked Moved Java
javadesigntutorialannouncementlounge
11 Posts 3 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Bactos

    Hi am new to Java and programming in general. I have been trying to work on a project for school of which I have already posted ( a horrible version of the "program") though would just like to understand what I have done wrong. I don't like not knowing how to do stuff... here is what I have posted : /* Write the program in Java (without a graphical user interface) using a loan amount of $200,000 with an interest rate of 5.75% and a 30 year term. Display the mortgage payment amount and then list the loan balance and interest paid for each payment over the term of the loan. If the list would scroll off the screen, use loops to display a partial list, hesitate, and then display more of the list. */ package cr1; import java.text.DecimalFormat; /** * * @author */ public class CR1 { /** * @param args the command line arguments */ public static void main(String[] args) { DecimalFormat dollarAmount = new DecimalFormat("0.00"); double loan_amount = 200000.00; int years = 30; // calculation number of payments int number_of_payments = years*12; double intrest_rate = 5.75; double monthly_intrest_rate = intrest_rate/100/12; // calculates monthly inrest rate // monthly payment double monthly_payment = monthly_intrest_rate/(1 - Math.pow(1+monthly_intrest_rate,-number_of_payments)) * loan_amount; double ipaid = monthly_payment * .0575; int paymentnumber = 0; int payCounter = 0; int pCounter = 0; double loanbalance = loan_amount - number_of_payments; while (loanbalance > 0) // if (payCounter == 0) { System.out.println("Starting loan balance: $" + dollarAmount.format(loan_amount)); System.out.println(""); payCounter += 1; pCounter += 1; } else { System.out.println("Payment Number:" + payCounter); System.out.println("Monthly Payment:$"+dollarAmount.format (monthly_payment)); System.out.println("Loan Balance: $" +dollarAmount.format (loanbalance)); System.out.println("Intrest Paid:$ " +dollarAmount.format (ipaid)); loanbalance = loanbalance - monthly_payment; payCounter ++; pCounter += 1; } if (pCounter ==9 ) { try { Thread.sleep(200000); pCounter = 0; } catch(InterruptedException CR1) { // message prints with Java-generated

    F Offline
    F Offline
    fly904
    wrote on last edited by
    #2

    Have you tried debugging it? ps. Your not meant to put School Work on here as Chris Maunder Said at the top: "9. If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums."

    hmmm pie

    B 1 Reply Last reply
    0
    • F fly904

      Have you tried debugging it? ps. Your not meant to put School Work on here as Chris Maunder Said at the top: "9. If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums."

      hmmm pie

      B Offline
      B Offline
      Bactos
      wrote on last edited by
      #3

      That is not a big deal as I have said I have already posted and will receive a grade for the work above. I just want to understand what it is that I have done wrong. My teacher can read this forum and it will not be a bad thing. I am not asking for people to do my work, just help me understand what I am messing up on. I do not like not knowing or understanding something. I have looked in tutorials for Java, and found lots of information however, none that will help me explain my issues that I am having. i.e. Am I using the wrong type of loop, not structuring it right or leaving something out. However I completely understand if no one wants to look at it because its a school assignment.

      Bactos

      F 1 Reply Last reply
      0
      • B Bactos

        Hi am new to Java and programming in general. I have been trying to work on a project for school of which I have already posted ( a horrible version of the "program") though would just like to understand what I have done wrong. I don't like not knowing how to do stuff... here is what I have posted : /* Write the program in Java (without a graphical user interface) using a loan amount of $200,000 with an interest rate of 5.75% and a 30 year term. Display the mortgage payment amount and then list the loan balance and interest paid for each payment over the term of the loan. If the list would scroll off the screen, use loops to display a partial list, hesitate, and then display more of the list. */ package cr1; import java.text.DecimalFormat; /** * * @author */ public class CR1 { /** * @param args the command line arguments */ public static void main(String[] args) { DecimalFormat dollarAmount = new DecimalFormat("0.00"); double loan_amount = 200000.00; int years = 30; // calculation number of payments int number_of_payments = years*12; double intrest_rate = 5.75; double monthly_intrest_rate = intrest_rate/100/12; // calculates monthly inrest rate // monthly payment double monthly_payment = monthly_intrest_rate/(1 - Math.pow(1+monthly_intrest_rate,-number_of_payments)) * loan_amount; double ipaid = monthly_payment * .0575; int paymentnumber = 0; int payCounter = 0; int pCounter = 0; double loanbalance = loan_amount - number_of_payments; while (loanbalance > 0) // if (payCounter == 0) { System.out.println("Starting loan balance: $" + dollarAmount.format(loan_amount)); System.out.println(""); payCounter += 1; pCounter += 1; } else { System.out.println("Payment Number:" + payCounter); System.out.println("Monthly Payment:$"+dollarAmount.format (monthly_payment)); System.out.println("Loan Balance: $" +dollarAmount.format (loanbalance)); System.out.println("Intrest Paid:$ " +dollarAmount.format (ipaid)); loanbalance = loanbalance - monthly_payment; payCounter ++; pCounter += 1; } if (pCounter ==9 ) { try { Thread.sleep(200000); pCounter = 0; } catch(InterruptedException CR1) { // message prints with Java-generated

        R Offline
        R Offline
        Reagan Conservative
        wrote on last edited by
        #4

        Bactos wrote:

        loanbalance = loanbalance - monthly_payment;

        This is not what you want. You need something like [code] loanbalance = loanbalance - "principal" (which is part of your loan payment). [/code] So you need to figure out your loan payment (which is comprised of interest AND principal); You need to determine what part of that loan payment is pricipal and subtract it from the loan balance. This needs to be done in a loop for every month (30 years time 12 months = 360). I can guarantee you this will run off the screen page, so you need to handle that portion. An amortized loan will have large interest and small principal in the early months. In the latter months, the opposite will be true; less interest and more principal as the parts of your loan payment. Just ask anyone who has a 30 year mortgage!

        AF Pilot

        B 1 Reply Last reply
        0
        • B Bactos

          That is not a big deal as I have said I have already posted and will receive a grade for the work above. I just want to understand what it is that I have done wrong. My teacher can read this forum and it will not be a bad thing. I am not asking for people to do my work, just help me understand what I am messing up on. I do not like not knowing or understanding something. I have looked in tutorials for Java, and found lots of information however, none that will help me explain my issues that I am having. i.e. Am I using the wrong type of loop, not structuring it right or leaving something out. However I completely understand if no one wants to look at it because its a school assignment.

          Bactos

          F Offline
          F Offline
          fly904
          wrote on last edited by
          #5

          Again, have you tried Debugging?

          hmmm pie

          B 1 Reply Last reply
          0
          • F fly904

            Again, have you tried Debugging?

            hmmm pie

            B Offline
            B Offline
            Bactos
            wrote on last edited by
            #6

            Yes I have tried debugging it though at the moment it does not have any errors. The code works though I cannot produce the output that I would like. It is producing the loan amount, the amount left on the loan and everything correct. It is having an issue with fulfilling the appropriate count for number of payments. It only goes to 172 payments instead of the 360 payments ( for the 30 year loan). I have tried using a "for" loop though all the examples I have looked at online seem straightforward however when I try it in (netbeans which is my compiler) it is giving me an error and will not let it compile. I do : for (int payCounter =0; payCounter => 359; payCounter++) { //code } It is telling me that a boolean is required and an int is not allowed, as well as an illegal start of expression. As a side note I am not quite sure if it is my compiler or Java in general, can you not name a variable twice? I have tired to use say "payCounter" in the main code as well as in a statement though it will not let me compile saying it was already defined. i.e. main{ payCounter = 0; //more variables for (int payCounter =0; payCounter => 359; payCounter++) { //code } Though if I take out the veriable in the main when I use it agian in the "for" loop it says that it is undefined.

            Bactos

            F 1 Reply Last reply
            0
            • R Reagan Conservative

              Bactos wrote:

              loanbalance = loanbalance - monthly_payment;

              This is not what you want. You need something like [code] loanbalance = loanbalance - "principal" (which is part of your loan payment). [/code] So you need to figure out your loan payment (which is comprised of interest AND principal); You need to determine what part of that loan payment is pricipal and subtract it from the loan balance. This needs to be done in a loop for every month (30 years time 12 months = 360). I can guarantee you this will run off the screen page, so you need to handle that portion. An amortized loan will have large interest and small principal in the early months. In the latter months, the opposite will be true; less interest and more principal as the parts of your loan payment. Just ask anyone who has a 30 year mortgage!

              AF Pilot

              B Offline
              B Offline
              Bactos
              wrote on last edited by
              #7

              I thought that it would be more complicated and so at the start of the assignment I had started doing it with amortized loan though it is supposed to me a simple interest. I think the point of the assignment is not the understanding of the morgage system as much as understanding loops and repeating. I will mess around with it with the advice and post back in a little while. ~at work so I dont know what a little while will be.

              Bactos

              B 1 Reply Last reply
              0
              • B Bactos

                Yes I have tried debugging it though at the moment it does not have any errors. The code works though I cannot produce the output that I would like. It is producing the loan amount, the amount left on the loan and everything correct. It is having an issue with fulfilling the appropriate count for number of payments. It only goes to 172 payments instead of the 360 payments ( for the 30 year loan). I have tried using a "for" loop though all the examples I have looked at online seem straightforward however when I try it in (netbeans which is my compiler) it is giving me an error and will not let it compile. I do : for (int payCounter =0; payCounter => 359; payCounter++) { //code } It is telling me that a boolean is required and an int is not allowed, as well as an illegal start of expression. As a side note I am not quite sure if it is my compiler or Java in general, can you not name a variable twice? I have tired to use say "payCounter" in the main code as well as in a statement though it will not let me compile saying it was already defined. i.e. main{ payCounter = 0; //more variables for (int payCounter =0; payCounter => 359; payCounter++) { //code } Though if I take out the veriable in the main when I use it agian in the "for" loop it says that it is undefined.

                Bactos

                F Offline
                F Offline
                fly904
                wrote on last edited by
                #8

                With debugging you go through the code line by line and have a look at what the variables values are. Generally in a for loop when you declare the variable it shouldn't be used elsewhere. The convention is normally to use "i" but it is not necesserily the case all the time.

                Bactos wrote:

                for (int payCounter =0; payCounter => 359; payCounter++)

                Should be:

                for (int i = 0; i < 360; i++)

                As the loop will only run while i is less than 360 rather than payCounter having to be greater or equal to 359 which would never execute the loop as it starts as 0.

                hmmm pie

                B 1 Reply Last reply
                0
                • F fly904

                  With debugging you go through the code line by line and have a look at what the variables values are. Generally in a for loop when you declare the variable it shouldn't be used elsewhere. The convention is normally to use "i" but it is not necesserily the case all the time.

                  Bactos wrote:

                  for (int payCounter =0; payCounter => 359; payCounter++)

                  Should be:

                  for (int i = 0; i < 360; i++)

                  As the loop will only run while i is less than 360 rather than payCounter having to be greater or equal to 359 which would never execute the loop as it starts as 0.

                  hmmm pie

                  B Offline
                  B Offline
                  Bactos
                  wrote on last edited by
                  #9

                  HAHA!... Wow I cant believe i did not see that. I always switch <, > around. I understand that it can not => or <= now and assume that is why i keept getting the error. Though I have been looking at switching compilers because I don't get an error code, like I do in my .Net . Because the error codes are easier to look up. So I am able to get my statement working with for loop and now understand why it was not working in the first place. (because i am a dolt). I do think that something is off with my math so I will keep plugging away until I get it perfect. As I have said before, I don't like not understanding things. Thanks,

                  Bactos

                  B 1 Reply Last reply
                  0
                  • B Bactos

                    HAHA!... Wow I cant believe i did not see that. I always switch <, > around. I understand that it can not => or <= now and assume that is why i keept getting the error. Though I have been looking at switching compilers because I don't get an error code, like I do in my .Net . Because the error codes are easier to look up. So I am able to get my statement working with for loop and now understand why it was not working in the first place. (because i am a dolt). I do think that something is off with my math so I will keep plugging away until I get it perfect. As I have said before, I don't like not understanding things. Thanks,

                    Bactos

                    B Offline
                    B Offline
                    Bactos
                    wrote on last edited by
                    #10

                    Ok so here is what I have now.... ////Write the program in Java (without a graphical user interface) //// using a loan amount of $200,000 with an interest rate of 5.75% and //// a 30 year term. Display the mortgage payment amount and then list the //// loan balance and interest paid for each payment over the term of the loan. //// If the list would scroll off the screen, use loops to display a partial list, //// hesitate, and then display more of the list. //// */ //// import java.text.DecimalFormat; public class CR1 { /** * @param args the command line arguments */ public static void main(String[] args) { DecimalFormat dollarAmount = new DecimalFormat("0.00"); double loan_amount = 200000.00; // amount of the loan int terminyears = 30; // term of the loan in years int number_of_payments = terminyears*12; // sets double intrest_rate = 5.75; // intrest rate of the loan double monthly_intrest_rate = intrest_rate/100/12; // calculates monthly inrest rate double loanbalance = loan_amount - number_of_payments; double monthly_payment = monthly_intrest_rate/(1 - Math.pow(1+monthly_intrest_rate,-number_of_payments)) * loan_amount; double P = loan_amount; double M = monthly_payment; double J = monthly_intrest_rate; int N = number_of_payments; //double ipaid = monthly_payment * .0575; int pCounter = 0; // sets counter to zero for (int i =0; i < 359; i++) { double H = P * J; double C = M - H; double Q = P- C; P=Q; System.out.println("Payment Number:" +i); System.out.println("Monthly Payment:$"+ dollarAmount.format (M)); System.out.println("Loan Balance: $" + dollarAmount.format (Q)); System.out.println("Intrest Paid:$ " + dollarAmount.format (H)); System.out.println(); pCounter += 0; }//end for if (pCounter ==9 ) { try { Thread.sleep(20000000); pCounter = 0; }// end try catch(InterruptedException e) { // }// end catch }// end if if (loanbalance < 0) { System.out.println("Than you for using Billy's Program. Have a good DAY!!!"); System.exit(0); }// end else } // end main }// end program Everything works!!.. Thanks for your help.

                    Bactos

                    1 Reply Last reply
                    0
                    • B Bactos

                      I thought that it would be more complicated and so at the start of the assignment I had started doing it with amortized loan though it is supposed to me a simple interest. I think the point of the assignment is not the understanding of the morgage system as much as understanding loops and repeating. I will mess around with it with the advice and post back in a little while. ~at work so I dont know what a little while will be.

                      Bactos

                      B Offline
                      B Offline
                      Bactos
                      wrote on last edited by
                      #11

                      You were right...no matter how much I got the loops right and all if i did not have the loan amortized it would not work right. Thanks

                      Bactos

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups