Join 244,072 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,321 people online right now. Registration is fast and FREE... Join Now!
public static void main(String[] args){ //Declares and builds the variable of the loan amount int loanAmount = 200000; double loanBalance = 0; double interestPaid = 0; int loanTerm[] = {84, 180, 360}; double interest[] = {.0535, .0550, .0575}; double monthlyPayment = 0; DecimalFormat money =new DecimalFormat("0.00");
//Starts array loop for (int i = 0; i < 3; i++) { //Declares formula monthlyPayment = (loanAmount*(interest[i]/12)) / (1 - 1 /Math.pow((1 + interest[i]/12), loanTerm[i]));
//Displays loop with first mortgage and interest paid if (i == 1) monthlyPayment = (loanAmount*(interest[0]/12)) / (1 - 1 /Math.pow((1 + interest[0]/12), loanTerm[0])); loanBalance = loanBalance - monthlyPayment; interestPaid = monthlyPayment * interest[0];
while (loanBalance > 0){
//Displays Mortgage Calculator with the first terms System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[0]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();
//Displays loop with second mortgage and interest paid if (i == 2) while (loanBalance > 0){ //Declares formulas monthlyPayment = (loanAmount*(interest[1]/12)) / (1 - 1 /Math.pow((1 + interest[1]/12), loanTerm[1])); loanBalance = loanAmount - monthlyPayment; interestPaid = monthlyPayment * interest[1];
//Displays Mortgage Calculator with the first terms until balance equals zero System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[1]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();}
//Displays loop with third mortgage and interest paid if (i == 3) while (loanBalance > 0){ //Declares formulas monthlyPayment = (loanAmount*(interest[2]/12)) / (1 - 1 /Math.pow((1 + interest[2]/12), loanTerm[2])); loanBalance = loanAmount - monthlyPayment; interestPaid = monthlyPayment * interest[2];
//Displays Mortgage Calculator with the first terms until balance equals zero System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[2]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();} } } }
For some reason this calculator does not run. It compiles successfully but when I run it all I get as an output is "press any key to continue" I know I am missing something stupid here. Any help would be great.
public static void main(String[] args){ //Declares and builds the variable of the loan amount int loanAmount = 200000; double loanBalance = 0; double interestPaid = 0; int loanTerm[] = {84, 180, 360}; double interest[] = {.0535, .0550, .0575}; double monthlyPayment = 0; DecimalFormat money =new DecimalFormat("0.00");
//Starts array loop for (int i = 0; i < 3; i++) { //Declares formula monthlyPayment = (loanAmount*(interest[i]/12)) / (1 - 1 /Math.pow((1 + interest[i]/12), loanTerm[i]));
//Displays loop with first mortgage and interest paid if (i == 1) monthlyPayment = (loanAmount*(interest[0]/12)) / (1 - 1 /Math.pow((1 + interest[0]/12), loanTerm[0])); loanBalance = loanBalance - monthlyPayment; interestPaid = monthlyPayment * interest[0];
while (loanBalance > 0){
//Displays Mortgage Calculator with the first terms System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[0]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();
//Displays loop with second mortgage and interest paid if (i == 2) while (loanBalance > 0){ //Declares formulas monthlyPayment = (loanAmount*(interest[1]/12)) / (1 - 1 /Math.pow((1 + interest[1]/12), loanTerm[1])); loanBalance = loanAmount - monthlyPayment; interestPaid = monthlyPayment * interest[1];
//Displays Mortgage Calculator with the first terms until balance equals zero System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[1]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();}
//Displays loop with third mortgage and interest paid if (i == 3) while (loanBalance > 0){ //Declares formulas monthlyPayment = (loanAmount*(interest[2]/12)) / (1 - 1 /Math.pow((1 + interest[2]/12), loanTerm[2])); loanBalance = loanAmount - monthlyPayment; interestPaid = monthlyPayment * interest[2];
//Displays Mortgage Calculator with the first terms until balance equals zero System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[2]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();} } } }
For some reason this calculator does not run. It compiles successfully but when I run it all I get as an output is "press any key to continue" I know I am missing something stupid here. Any help would be great.
Ok. I think the program is running I have just forgotten to pause the display. However, I am now having a hard time getting the pause to work.
public static void main(String[] args){ //Declares and builds the variable of the loan amount int loanAmount = 200000; double loanBalance = 0; double interestPaid = 0; int loanTerm[] = {84, 180, 360}; double interest[] = {.0535, .0550, .0575}; double monthlyPayment = 0; DecimalFormat money =new DecimalFormat("0.00");
//Starts array loop for (int i = 0; i < 3; i++) { //Declares formula monthlyPayment = (loanAmount*(interest[i]/12)) / (1 - 1 /Math.pow((1 + interest[i]/12), loanTerm[i]));
//Displays loop with first mortgage and interest paid if (i == 1) monthlyPayment = (loanAmount*(interest[0]/12)) / (1 - 1 /Math.pow((1 + interest[0]/12), loanTerm[0])); loanBalance = loanBalance - monthlyPayment; interestPaid = monthlyPayment * interest[0];
while (loanBalance > 0){
//Displays Mortgage Calculator with the first terms System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[0]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();
//Displays loop with second mortgage and interest paid if (i == 2) while (loanBalance > 0){ //Declares formulas monthlyPayment = (loanAmount*(interest[1]/12)) / (1 - 1 /Math.pow((1 + interest[1]/12), loanTerm[1])); loanBalance = loanAmount - monthlyPayment; interestPaid = monthlyPayment * interest[1];
//Displays Mortgage Calculator with the first terms until balance equals zero System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[1]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();}
//Displays loop with third mortgage and interest paid if (i == 3) while (loanBalance > 0){ //Declares formulas monthlyPayment = (loanAmount*(interest[2]/12)) / (1 - 1 /Math.pow((1 + interest[2]/12), loanTerm[2])); loanBalance = loanAmount - monthlyPayment; interestPaid = monthlyPayment * interest[2];
//Displays Mortgage Calculator with the first terms until balance equals zero System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[2]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();}
} } } //Pauses screen if (lineCount ==15) { lineCount--;
The way the program is written nothing will be printed out. The if statements do not have the { } brackets surrounding them so they will terminate as soon as they hit the next ';', anything after that will be eexcuted each time through the loop.
The loanBalance is also never initially set to anything besides 0, which means the while loops will never execute, the loan balance needs to be reset each time through the loop at each of the if statements.
The statement loanBalance = loanAmount - monthlyPayment; in each of the while loops should be loanBalance = loanBalance - monthlyPayment; in the first statement the loanBalance will be reset to 200000 resulting in an infinite loop.
Also the if statements should start at if (i == 0) and go to if(i == 2) in the for loop you have if(i == 3) will never be executed cause i will never be greater than 2
The way the program is written nothing will be printed out. The if statements do not have the { } brackets surrounding them so they will terminate as soon as they hit the next ';', anything after that will be eexcuted each time through the loop.
The loanBalance is also never initially set to anything besides 0, which means the while loops will never execute, the loan balance needs to be reset each time through the loop at each of the if statements.
The statement loanBalance = loanAmount - monthlyPayment; in each of the while loops should be loanBalance = loanBalance - monthlyPayment; in the first statement the loanBalance will be reset to 200000 resulting in an infinite loop.
Also the if statements should start at if (i == 0) and go to if(i == 2) in the for loop you have if(i == 3) will never be executed cause i will never be greater than 2
Thank you. I am confused about the brackets with the if statements. I did change the loanBalance statements. I am really confused at this tme. This asssignment is do today and I am getting more confused by the minute.
public static void main(String[] args){ //Declares and builds the variable of the loan amount int loanAmount = 200000; double loanBalance = 0; double interestPaid = 0; int loanTerm[] = {84, 180, 360}; double interest[] = {.0535, .0550, .0575}; double monthlyPayment = 0; DecimalFormat money =new DecimalFormat("0.00");
//Starts array loop for (int i = 0; i < 3; i++) { //Declares formula monthlyPayment = (loanAmount*(interest[i]/12)) / (1 - 1 /Math.pow((1 + interest[i]/12), loanTerm[i]));
//Displays loop with first mortgage and interest paid if (i == 1){ monthlyPayment = (loanAmount*(interest[0]/12)) / (1 - 1 /Math.pow((1 + interest[0]/12), loanTerm[0])); loanBalance = loanBalance - monthlyPayment; interestPaid = monthlyPayment * interest[0];}
while (loanBalance > 0){
//Displays Mortgage Calculator with the first terms System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[0]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();
//Displays loop with second mortgage and interest paid if (i == 2) while (loanBalance > 0){ //Declares formulas monthlyPayment = (loanAmount*(interest[1]/12)) / (1 - 1 /Math.pow((1 + interest[1]/12), loanTerm[1])); loanBalance = loanBalance - monthlyPayment; interestPaid = monthlyPayment * interest[1];
//Displays Mortgage Calculator with the first terms until balance equals zero System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[1]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();}
//Displays loop with third mortgage and interest paid if (i == 3) while (loanBalance > 0){ //Declares formulas monthlyPayment = (loanAmount*(interest[2]/12)) / (1 - 1 /Math.pow((1 + interest[2]/12), loanTerm[2])); loanBalance = loanBalance - monthlyPayment; interestPaid = monthlyPayment * interest[2];
//Displays Mortgage Calculator with the first terms until balance equals zero System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[2]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();} } } }
//Displays loop with first mortgage and interest paid if (i == 1){ monthlyPayment = (loanAmount*(interest[0]/12)) / (1 - 1 /Math.pow((1 + interest[0]/12), loanTerm[0])); loanBalance = loanBalance - monthlyPayment; interestPaid = monthlyPayment * interest[0];}
while (loanBalance > 0){
//Displays Mortgage Calculator with the first terms System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[0]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();
if (i == 1) { monthlyPayment = (loanAmount*(interest[0]/12)) / (1 - 1 /Math.pow((1 + interest[0]/12), loanTerm[0])); loanBalance = loanBalance - monthlyPayment; interestPaid = monthlyPayment * interest[0]; //} If the if end here the while loop will execute each time the for loop loops while (loanBalance > 0){
//Displays Mortgage Calculator with the first terms System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[0]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();
}//End if The while loop will execute only with the if statement
The { } brackets surround what should be executed with the if statement. If you are confused about where to put them i would reccomended to label the { } brackets to indicate which ones are which and to indent the code to make it easier to follow. These changes need to be done to all three if statements to get the loops to execute when you want them to.
Also change the boolean statements in the if statements to be from 0-2 insteaed of 1-3 or else all the loops will not execute.
//Displays loop with first mortgage and interest paid if (i == 1){ monthlyPayment = (loanAmount*(interest[0]/12)) / (1 - 1 /Math.pow((1 + interest[0]/12), loanTerm[0])); loanBalance = loanBalance - monthlyPayment; interestPaid = monthlyPayment * interest[0];}
while (loanBalance > 0){
//Displays Mortgage Calculator with the first terms System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[0]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();
if (i == 1) { monthlyPayment = (loanAmount*(interest[0]/12)) / (1 - 1 /Math.pow((1 + interest[0]/12), loanTerm[0])); loanBalance = loanBalance - monthlyPayment; interestPaid = monthlyPayment * interest[0]; //} If the if end here the while loop will execute each time the for loop loops while (loanBalance > 0){
//Displays Mortgage Calculator with the first terms System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[0]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();
}//End if The while loop will execute only with the if statement
The { } brackets surround what should be executed with the if statement. If you are confused about where to put them i would reccomended to label the { } brackets to indicate which ones are which and to indent the code to make it easier to follow. These changes need to be done to all three if statements to get the loops to execute when you want them to.
Also change the boolean statements in the if statements to be from 0-2 insteaed of 1-3 or else all the loops will not execute.
Ok. By the way I appreciate your patience. I think I did what you requested. Now I get the error "cannot resolve symbol symbol: variable1 if(i==2){}
public static void main(String[] args){ //Declares and builds the variable of the loan amount int loanAmount = 200000; double loanBalance = 0; double interestPaid = 0; int loanTerm[] = {84, 180, 360}; double interest[] = {.0535, .0550, .0575}; double monthlyPayment = 0; DecimalFormat money =new DecimalFormat("0.00");
//Starts array loop for (int i = 0; i < 2; i++) { //Declares formula monthlyPayment = (loanAmount*(interest[i]/12)) / (1 - 1 /Math.pow((1 + interest[i]/12), loanTerm[i]));
//Displays loop with first mortgage and interest paid if (i == 0){ monthlyPayment = (loanAmount*(interest[0]/12)) / (1 - 1 /Math.pow((1 + interest[0]/12), loanTerm[0])); loanBalance = loanBalance - monthlyPayment; interestPaid = monthlyPayment * interest[0];}
while (loanBalance > 0){
//Displays Mortgage Calculator with the first terms System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[0]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();
//Displays loop with second mortgage and interest paid if (i == 1){} while (loanBalance > 0){ //Declares formulas monthlyPayment = (loanAmount*(interest[1]/12)) / (1 - 1 /Math.pow((1 + interest[1]/12), loanTerm[1])); loanBalance = loanBalance - monthlyPayment; interestPaid = monthlyPayment * interest[1];}
//Displays Mortgage Calculator with the first terms until balance equals zero System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[1]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();}
//Displays loop with third mortgage and interest paid if (i == 2){} while (loanBalance > 0){ //Declares formulas monthlyPayment = (loanAmount*(interest[2]/12)) / (1 - 1 /Math.pow((1 + interest[2]/12), loanTerm[2])); loanBalance = loanBalance - monthlyPayment; interestPaid = monthlyPayment * interest[2];
//Displays Mortgage Calculator with the first terms until balance equals zero System.out.println("Mortgage Calculator"); System.out.println("The loan amount is $200,000"); System.out.println("The term of the loan is " + loanTerm[2]); System.out.println("The monthly payment is $" + money.format(monthlyPayment)); System.out.println("The loan balance is: $" + money.format(loanBalance)); System.out.println("The interest paid for the loan is $" + money.format(interestPaid)); System.out.println();} } }
Edited to add =java to the code tag... that might help to align the {}
This post has been edited by pbl: 7 Sep, 2008 - 06:09 PM
You have to put the statements you want executed when the if boolean statement is true between the { } braces. If you have if(i == 0) { } then no statements will be executed when the if statements is true because there are no statements between the { } braces. The first if statement is shown in my previous posted code. Follow this to format the last 2 if statements.
After that at the beginning of each if statement (before anything else) you should set loanBalance = loanAmmount; to initialize the loanBalance before the while loop.
This post has been edited by BigAnt: 7 Sep, 2008 - 01:53 PM
You have to put the statements you want executed when the if boolean statement is true between the { } braces. If you have if(i == 0) { } then no statements will be executed when the if statements is true because there are no statements between the { } braces. The first if statement is shown in my previous posted code. Follow this to format the last 2 if statements.
After that at the beginning of each if statement (before anything else) you should set loanBalance = loanAmmount; to initialize the loanBalance before the while loop.