I am stuck again, I was able to complete last week's assignment without having to post, just so you guys don't think I'm a lost cause! :) I need to allow the user to input loan amount, term, and interest rate before calculating the monthly payment and showing the table. This is what I have so far. I am getting between two and three errors that I cannot figure out. Any help and/or hints are greatly appreciated.

``````/*
MortgageProgramWK4b.java
This program will accept user input for the loan amount,
interest rate, and term of the loan.  Then calculate and
display the monthly mortgage payment amount, Followed by,
listing the loan balance and interest paid for
each payment over the term of the loan.
*/

import java.math.*;
import java.io.*;

public static void main(String[] args)throws Exception
{

//local objects
MortgageProgramWK4b mortgageCalc = new MortgageWK4b();
DecimalFormat df = new DecimalFormat("###,##0.00");
DecimalFormat numDf = new DecimalFormat("000");

int numOfYears;
double annualInterest;
double principal;
double monthlyPayment;

//Display Title
System.out.println("Mortgage Calculator");
System.out.println();

//Get User Input of principal
int principal = readInterger("Please enter the pricipal amount of the loan." );

//Get User Input of rate
int annualInterest = readInterger("Please enter the pricipal amount of the loan." );

//Get User Input of Term
int numOfYears = readInterger("Please enter the pricipal amount of the loan." );

double monthlyInterest = annualInterest/(12*100);!
int totalNumOfMonths = numOfYears*12;
System.out.println("Payment#   LoanBalance    InterestPaid");
int monthCounter=1;
for(; totalNumOfMonths>0; totalNumOfMonths--){
//first get the monthly payment...
double monthlyPayment = mortgageCalc.getMortgageAmount(principal,monthlyInterest,totalNumOfMonths);

double monthlyPrincipal = mortgageCalc.getMonthlyPrincipal(monthlyPayment,monthlyInterest,principal);
principal-=monthlyPrincipal;
System.out.println(numDf.format(monthCounter) + "          " +df.format(principal)+ "          " +df.format((monthlyPayment-monthlyPrincipal)));
monthCounter++;
if(monthCounter%25==0)sleep(1000); // this will hesitate...
}
}

public double getMortgageAmount(double principal, double monthlyInterest, int numOfMonths){
// Monthly payment formula: M = P x (J/(1-(1+J)^-N));
// M = P * ( J / (1 - (1 + J) ** -N));
// source: http://www.hughchou.org/calc/formula.html
double monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1))))));
return monthlyPayment;
}

//calculate the monthly interst using simple interest.
public double getMonthlyPrincipal(double monthlyPayment, double monthlyInterest, double remainingPrincipal){
return monthlyPayment - (remainingPrincipal * monthlyInterest);
}

// method to make the program sleep (hesitate) for a give time (in milliseconds)..
public static void sleep(long milliseconds){
try{
}catch(Exception e){

}
}

}``````

_____________________________

This is the other way I tried it... I am getting the same three errors both ways...

``````/*
MortgageProgramWK4a.java
This program will accept user input for the loan amount,
interest rate, and term of the loan.  Then calculate and
display the monthly mortgage payment amount, Followed by,
listing the loan balance and interest paid for
each payment over the term of the loan.
*/

import java.math.*;
import java.io.*;

public static void main(String[] args)throws Exception
{

//local objects
MortgageProgramWK4a mortgageCalc = new MortgageProgramWK4a();
DecimalFormat df = new DecimalFormat("###,##0.00");
DecimalFormat numDf = new DecimalFormat("000");

int numOfYears;
double annualInterest;
double principal;
double monthlyPayment;

//Display Title
System.out.println("Mortgage Calculator");
System.out.println();

//Get User Input of principal
System.out.println("Please enter the loan amount: ");

//Get User Input of rate
System.out.println("Please enter the interest rate: ");

//Get User Input of Term
System.out.println("Please enter the term of the loan in years: ");

int numOfYears    = System.in();
double monthlyInterest = annualInterest/(12*100);!
int totalNumOfMonths = numOfYears*12;
System.out.println("Payment#   LoanBalance    InterestPaid");
int monthCounter=1;
for(; totalNumOfMonths>0; totalNumOfMonths--){
//first get the monthly payment...
double monthlyPayment = mortgageCalc.getMortgageAmount(principal,monthlyInterest,totalNumOfMonths);
//now get the monthly principal in the payment...
double monthlyPrincipal = mortgageCalc.getMonthlyPrincipal(monthlyPayment,monthlyInterest,principal);
principal-=monthlyPrincipal;

System.out.println(numDf.format(monthCounter) + "          " +df.format(principal)+ "          " +df.format((monthlyPayment-monthlyPrincipal)));
monthCounter++;
if(monthCounter%25==0)sleep(1000); // this will hesitate...
}
}

public double getMortgageAmount(double principal, double monthlyInterest, int numOfMonths){
// Monthly payment formula: M = P x (J/(1-(1+J)^-N));
// M = P * ( J / (1 - (1 + J) ** -N));
// source: http://www.hughchou.org/calc/formula.html
double monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1))))));
return monthlyPayment;
}

//calculate the monthly interst using simple interest.
public double getMonthlyPrincipal(double monthlyPayment, double monthlyInterest, double remainingPrincipal){
return monthlyPayment - (remainingPrincipal * monthlyInterest);
}

// method to make the program sleep (hesitate) for a give time (in milliseconds)..
public static void sleep(long milliseconds){
try{
}catch(Exception e){

}
}

}``````

I am getting the same three errors, all of them are saying "class" or "interface" expected. Again, any help is really appreciated.

Well, you don't define a class at all - just a main() method. Methods can't exist on their own, they must be in a class. You also don't define the readInterger (sic, misspelled Integer) method either, so you can't call it until you define it.

I feel like a goober for forgetting the class. I tried to define the readInteger() but I still can't make this work. Any other suggestions?

``````/*
MortgageProgramWK4b.java
This program will accept user input for the loan amount,
interest rate, and term of the loan.  Then calculate and
display the monthly mortgage payment amount, Followed by,
listing the loan balance and interest paid for
each payment over the term of the loan.
*/

import java.math.*;
import java.io.*;

class MortgageProgramWK4b
{
public static void main(String[] args)throws Exception

{
public boolean ReadInteger(String Key, String ValueName, int DefValue,Integer Value);
{
//local objects
MortgageProgramWK4b mortgageCalc = new MortgageWK4b();
DecimalFormat df = new DecimalFormat("###,##0.00");
DecimalFormat numDf = new DecimalFormat("000");

int numOfYears;
double annualInterest;
double principal;
double monthlyPayment;

//Display Title
System.out.println("Mortgage Calculator");
System.out.println();

//Get User Input of principal
int principal = readInterger("Please enter the pricipal amount of the loan." );

//Get User Input of rate
int annualInterest = readInterger("Please enter the pricipal amount of the loan." );

//Get User Input of Term
int numOfYears = readInterger("Please enter the pricipal amount of the loan." );

double monthlyInterest = annualInterest/(12*100);!
int totalNumOfMonths = numOfYears*12;
System.out.println("Payment#   LoanBalance    InterestPaid");
int monthCounter=1;
for(; totalNumOfMonths>0; totalNumOfMonths--){
//first get the monthly payment...
double monthlyPayment = mortgageCalc.getMortgageAmount(principal,monthlyInterest,totalNumOfMonths);

double monthlyPrincipal = mortgageCalc.getMonthlyPrincipal(monthlyPayment,monthlyInterest,principal);
principal-=monthlyPrincipal;
System.out.println(numDf.format(monthCounter) + "          " +df.format(principal)+ "          " +df.format((monthlyPayment-monthlyPrincipal)));
monthCounter++;
if(monthCounter%25==0)sleep(1000); // this will hesitate...
}
}

public double getMortgageAmount(double principal, double monthlyInterest, int numOfMonths){
// Monthly payment formula: M = P x (J/(1-(1+J)^-N));
// M = P * ( J / (1 - (1 + J) ** -N));
// source: http://www.hughchou.org/calc/formula.html
double monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1))))));
return monthlyPayment;
}

//calculate the monthly interst using simple interest.
public double getMonthlyPrincipal(double monthlyPayment, double monthlyInterest, double remainingPrincipal){
return monthlyPayment - (remainingPrincipal * monthlyInterest);
}

// method to make the program sleep (hesitate) for a give time (in milliseconds)..
public static void sleep(long milliseconds){
try{
}catch(Exception e){

}
}
;
}}``````

You cannot define methods within other methods, which is where you are trying to put ReadInteger. You have it declared in main(). Also, readInteger() should read an int input, which you must write the code for - just writing a declaration line won't do anything.

main has to be seperate function similar to your other functions... well here is the go.... main is also a regular function like your other function... but we have to write main function for something to begin with... your machine can compile all the functions... but if it finds main function, it takes that as a starting point.... any other methods you want to use, has to be linked with main by calling the function directly in main or in some other function, which was called in main.

How would I call the function in main without defining it as a method? I think I'm missing a pretty large chuck of the puzzle....

How would I call the function in main without defining it as a method? I think I'm missing a pretty large chuck of the puzzle....

He was trying to say that your method needs to be separate from main - not written inside it.

``````public class MyClass{

public static void main(String[] args){
someMethod();
}

public static void someMethod(){
// do some stuff
}
}``````

Okay, thanks for the help guys. I was really frustrated, therefore I started over from the begining. The good news is that I have it working... however, I need to be able to input a decimal for the annual interest input, if that is input it freaks out and quits, if a whole number is entered it works fine.

The only other issue I need to fix, it that I need to print the Montly Payment. I can make it print but I don't want it in the loop, because it prints over and over obviously. I can make it print to the screen anywhere else! Help!

``````/*
MortgageProgram.java
This program will accept user input for the loan amount,
interest rate, and term of the loan.  Then calculate and
display the monthly mortgage payment amount, Followed by,
listing the loan balance and interest paid for
each payment over the term of the loan.

*/

import java.io.*;
import java.text.*;

public class MortgageProgram{

public static void main(String[] args) throws IOException{
//local objects
MortgageWK3 mortgageCalc = new MortgageWK3();
DecimalFormat df = new DecimalFormat("###,##0.00"); // to format the amount.
DecimalFormat numDf = new DecimalFormat("000"); // to format the sequence number.

double principal = 0;
double annualInterest = 0.0;
int numOfYears = 0;

System.out.print("Please enter the amount borrowed:"); //prompt user input
String input = stdin.readLine(); //get user input
principal= Integer.parseInt(input); //convert string to int

annualInterest = Integer.parseInt(input2);

System.out.print("Please enter the term of the loan in years:");
numOfYears = Integer.parseInt(input3);

//variables
double monthlyInterest = annualInterest/(12*100);
int totalNumOfMonths = numOfYears*12;

System.out.println("Payment#   LoanBalance    InterestPaid");
int monthCounter=1;
for(; totalNumOfMonths>0; totalNumOfMonths--){
//first get the monthly payment...
double monthlyPayment = mortgageCalc.getMortgageAmount(principal,monthlyInterest,totalNumOfMonths);

//now get the monthly principal in the payment...
double monthlyPrincipal = mortgageCalc.getMonthlyPrincipal(monthlyPayment,monthlyInterest,principal);
principal-=monthlyPrincipal;

System.out.println(numDf.format(monthCounter) + "          " +df.format(principal)+ "          " +df.format((monthlyPayment-monthlyPrincipal)));
monthCounter++;
if(monthCounter%25==0)sleep(1000); // this will hesitate...

}

}

// this is from our last week.. All I did is move it to a function and give it a name
// the getMortgageAmount is the function name taking in the 3 arguments.
public double getMortgageAmount(double principal, double monthlyInterest, int numOfMonths){
// Monthly payment formula: M = P x (J/(1-(1+J)^-N));
// M = P * ( J / (1 - (1 + J) ** -N));
// source: http://www.hughchou.org/calc/formula.html
double monthlyPayment = (double)(principal*(monthlyInterest / (1-(Math.pow((1+monthlyInterest),(numOfMonths*-1))))));
return monthlyPayment;

}

//calculate the monthly interst using simple interest.
public double getMonthlyPrincipal(double monthlyPayment, double monthlyInterest, double remainingPrincipal){
return monthlyPayment - (remainingPrincipal * monthlyInterest);
}

// method to make the program sleep (hesitate) for a give time (in milliseconds)..
public static void sleep(long milliseconds){
try{
}catch(Exception e){
//do nothing... for now...
}
}

}``````

Thank again for all the input from before, y'alls help has been invaluable! : )

I have solved the decimal problem, I still haven't been able to figure out how to get the MonthlyPayment to print only once, before the loop, without being in the loop to print over and over again.

Just move the monthly payment call out of your loop and print it before the rest. Having it in the loop, you are actually changing the monthly payment on each iteration, which I don't believe you want to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.