• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Help with this mortgage calculation program

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on a mortgage calculation program and I need assistance with calling arrays.

my instructions are this:

Declare four one dimensional arrays to store three mortgages. Use the first array to store the amounts, the second one for Interest Rates, the third one for Years/months, and the last one to store the calculated payment for each loan.

Call a method to fill these arrays. You need to send the arrays to the method to get them filled. Call a second method to print the values from these arrays. Also, calculate and print the total payment for each mortgage. You need to send the arrays to the method for printing. Use a loop to print the values from the three arrays.

Fill Array Method

Accept the arrays as parameters and ask the user to input the values for each mortgage. Calculate the monthly payment for each mortgage and store it in the payment array. You need a loop here as we are dealing with three mortgages. Please check the output below to see the spacing between each loan.

Print Array Method

Accept the arrays as parameters and print the output as shown. Make sure the output is formatted.

and my code looks like this:

*/

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


public class MultipleLoans_2

{
public static void main(String[] args) throws IOException //Stores the exception rule

{
//declare the array using the following data types
double loanAmt[]=new double [3];//intializes loan amount array with assigned new double variable
double yearRate[]=new double [3];//intializes year rate array with assigned new double variable
double monthlyPmt[]=new double [3];//intializes monthly payment array with assigned new double variable
double numMonths []=new double [3];//intializes number of months array with assigned new double variable
double newInterest []=new double [3];//intializes new interest array with assigned new double variable
double totPmt []=new double [3];//initialize new total payment array with assigned new double variable
int loanTerm[]=new int [3];//intializes loan term with assigned new integer
int i;//declare function variable

DecimalFormat twoDigits = new DecimalFormat ("0.00");//Formats any specified amount with proper digits
String answer;//declare string variable named answer
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));//


for (i=0; i<3; i++)//creates a counter controlled loop
{
try//Note can not have a try statement without a finally or
{
System.out.print("Please enter the loan amount :$ ");//gets input from user
answer = dataIn.readLine();
loanAmt[i] = Double.parseDouble(answer);

System.out.print("What is the yearly interest rate(Eg. 5.0): ");
answer = dataIn.readLine();
yearRate[i] = Double.parseDouble(answer);

System.out.print("Enter the term in years: ");
answer = dataIn.readLine();
loanTerm[i] = Integer.parseInt(answer);

totPmt[i] = Double.parseDouble(answer);
System.out.print("\n");

while(loanAmt[i] <=0 && yearRate[i] <=0 && loanTerm[i] <=0);
throw new NumberFormatException();

} // Closes try statement

/*There are multiple components that must be analyzed in the above portion coding. The for loop loop creates a counter loop that will allow the user to the
loan amount,the yearly interest rate, and the loan term. The i=0 starts the loop, the i<2 is the stop point and the i++
increments the amount of times this particular portion of the program. The try gets user input including exceptions Inputs
are required from user. Program catches number format exception and catches any value that is negative. it
stores the error but continues through program for efficiency.
*/

catch (NumberFormatException nfe) //catches any incorrect information entered in the
{
System.out.print("\n");
System.out.println(" Input value not understood, Please input correct amount!");
System.out.print("\n");
System.out.print("\n");
}// This closes the Error output statement


}//This concludes the for loop


for (i=0; i<3; i++)//creates a new counter loop
//The formulas below Implement the calculation formulas to determine monthly payments
{
newInterest[i] = ((yearRate[i]/100)/12); //Convert interest rate to decimal

numMonths[i] = loanTerm[i]*12; // Convert yearly terms into months

monthlyPmt[i] = loanAmt[i]/((1/newInterest[i])-(1/(newInterest[i]*Math.pow(1+newInterest[i], numMonths[i]))));
//This formula calculates monthly payments for the mortgage plan

totPmt[i] = numMonths[i]*monthlyPmt[i];/* calculates the total payments based on
the number of months and monthly payments.*/

} // Needed to conclude this portion of the loop statement

System.out.print(" Loan\tRate\tMonths\tPayment\t\tTotpayment");
System.out.print("\n");
System.out.print("\n");

for (i=0; i<3; i++)//creates another counter loop statement
{

System.out.print("\n");
System.out.print(twoDigits.format(loanAmt[i])+"\t");//prints the loan amount and sends array specified row and column
System.out.print(twoDigits.format(yearRate[i])+"\t");// prints the year interest rate and sends array to its specified row and column
System.out.print(twoDigits.format(numMonths[i])+"\t");//prints the number of months for payment and sends array in its specified row and column
System.out.print(twoDigits.format(monthlyPmt[i])+ "\t\t");//prints the monthly payments and sends array its specified row and column
System.out.print(twoDigits.format(totPmt[i]));//prints the tot payment amount and sends array to its specified row and column
System.out.print("\n");

} // This concludes the for loop statement
}//Closes the main public static
}//Ends the public class file and ends program

am I addressing the instructions or no
 
Marshal
Posts: 80096
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please use "code" tags round quoted code; it makes it much easier to read.

I am pleased to see nice commenting, including the //end comments.
Have you been told to use old-fashioned input methods rather than the Scanner class?
Have you been told to use old-fashioned output formatting rather than the printf method of PrintStream?
The real problem you have got is your "while" loop. You have no body to it. Lots of people make this mistake, putting a ; after a while or an if. Sort that out and your app will work nicely. I can see no need for you to throw a NumberFormatException yourself; if there is an incorrect number that will happen automatically. Don't change the try and catch, however.

You are not displaying the payment correctly; I haven't checked whether you have calculated it, but the total payment column suggests that you have in fact calculated.
Use System.out.println(); rather than System.out.print("\n");
Put an additional tab in the top line of your output to improve formatting.
Look in a book for the way to loop through an array with
 
Campbell Ritchie
Marshal
Posts: 80096
413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But you haven't read the instructions. It says "Send the arrays to the methods to be filled." You will lose marks hand over fist. Please check that your app actually fulfills the instructions you have been given.
 
Dushayne Stevenson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that the instructions call for arrays to be initialized and to use methods to call the arrays. I have read up on this and I am getting very little input from my instructor on this. I could use some help formulating my program to reflect the instructions. My assignment is due tomarrow and I am about to jump. ARRRRRRGGGGG I am really starting to like Java but it can make you pull your hair out
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start with this: What would a method look like that takes an array as a parameter?
 
Dushayne Stevenson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Humm let me see,

getArray();
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the main method you're using.

  • "public" is the access modifier.
  • "static" associates the method with the class (rather than an instance of the class).
  • "void" is the return type (meaning that nothing is returned).
  • "main" is the method name.
  • "String[]" is the type of the parameter (an array of Strings).
  • "args" is the name of the parameter (the String array).
  • "//body" is what the method does.
  • So can you pattern a method after "main" to take an array that you want to process?
     
    Dushayne Stevenson
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I compile my code and I get one error that I do not understand

    class,interface, or enum expected
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Can you post the code?
     
    Dushayne Stevenson
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    import java.io.*;
    import java.text.*;


    public class MultipleLoans_2

    {

    public static void main(String[] args) throws IOException //Stores the exception rule
    {

    //declare the array using the following data types
    double loanAmt[]=new double [3];//intializes loan amount array with assigned new double variable
    double yearRate[]=new double [3];//intializes year rate array with assigned new double variable
    double monthlyPmt[]=new double [3];//intializes monthly payment array with assigned new double variable
    double numMonths[]=new double [3];//intializes number of months array with assigned new double variable
    double newInterest[]=new double [3];//intializes new interest array with assigned new double variable
    double totPmt[]=new double [3];//initialize new total payment array with assigned new double variable
    int loanTerm[]=new int [3];//intializes loan term with assigned new integer
    int i;//declare function variable

    String answer;//declare string variable named answer

    DecimalFormat twoDigits = new DecimalFormat ("0.00");//Formats any specified amount with proper digits
    BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));//

    }

    {
    getUserInput(loanAmt,yearRate,loanTerm);// call method for array for user input
    printValues(loanAmt,yearRate,monthlyPmt,numMonths,newInterest,totPmt,loanTerm);// call method for array print values
    }

    public static void getUserInput(double loanAmt[],double yearRte[],int loanTerm[])//fills array method
    {

    for (i=0; i<3; i++)//creates a counter controlled loop
    {
    try//Note can not have a try statement without a finally or
    {
    System.out.print("Please enter the loan amount :$ ");//gets input from user
    answer = dataIn.readLine();
    loanAmt[i] = Double.parseDouble(answer);

    System.out.print("What is the yearly interest rate(Eg. 5.0): ");
    answer = dataIn.readLine();
    yearRate[i] = Double.parseDouble(answer);

    System.out.print("Enter the term in years: ");
    answer = dataIn.readLine();
    loanTerm[i] = Integer.parseInt(answer);

    totPmt[i] = Double.parseDouble(answer);
    System.out.println();

    while(loanAmt[i] <=0 && yearRate[i] <=0 && loanTerm[i] <=0);

    } // Closes try statement

    catch (NumberFormatException nfe) //catches any incorrect information entered in the
    {
    System.out.println();
    System.out.println(" Input value not understood, Please input correct amount!");
    System.out.println();
    System.out.println();
    }// This closes the Error output statement


    }//This concludes the for loop
    return;
    }
    //print array method
    public static void printValues(double loanAmt[],double yearRate[],double monthlyPmt[],double numMonths[],
    double newInterest[],double totPmt[],int loanTerm[])
    {

    for (i=0; i<3; i++)//creates a new counter loop
    //The formulas below Implement the calculation formulas to determine monthly payments
    {
    newInterest[i] = ((yearRate[i]/100)/12); //Convert interest rate to decimal

    numMonths[i] = loanTerm[i]*12; // Convert yearly terms into months

    monthlyPmt[i] = loanAmt[i]/((1/newInterest[i])-(1/(newInterest[i]*Math.pow(1+newInterest[i], numMonths[i]))));
    //This formula calculates monthly payments for the mortgage plan

    totPmt[i] = numMonths[i]*monthlyPmt[i];/* calculates the total payments based on
    the number of months and monthly payments.*/

    } // Needed to conclude this portion of the loop statement

    System.out.print(" Loan\tRate\tMonths\tPayment\t\tTotpayment");
    System.out.println();
    System.out.println();

    for (i=0; i<3; i++)//creates another counter loop statement
    {

    System.out.println();
    System.out.print(twoDigits.format(loanAmt[i])+"\t");//prints the loan amount and sends array specified row and column
    System.out.print(twoDigits.format(yearRate[i])+"\t");// prints the year interest rate and sends array to its specified row and column
    System.out.print(twoDigits.format(numMonths[i])+"\t");//prints the number of months for payment and sends array in its specified row and column
    System.out.print(twoDigits.format(monthlyPmt[i])+ "\t\t");//prints the monthly payments and sends array its specified row and column
    System.out.print(twoDigits.format(totPmt[i]));//prints the tot payment amount and sends array to its specified row and column
    System.out.println();


    } // This concludes the for loop statement
    return;
    }
    }//Closes the main public static
    }//Ends the public class file and ends program
     
    Dushayne Stevenson
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    i appreciate all of the advice, I am approaching deadlines with this program and I just need to get it to compile already.
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Basically, your braces are not matching up. It would be a lot easier to see this with proper indentation and Code Tags.

    The compiler thinks the final closing brace at the end is an extra. So the compiler thinks you're done defining your class and is expecting you to start defining another class, interface, or enum instead of tossing in an extra brace. But the real cause of this problem appears to be much earlier in the code, just after declaring your variables...

    When you fix this, you will find more serious problems in the form of 52 compiler errors. The main reason for these is variable scope. You are defining a number of variables inside the main method, which means that their scope is limited to the main method body. Outside of main, none of these variables have any meaning. Instead of being local to the method, you probably want these variables to be instance variables, defined outside of the main method.

    But this is going to lead to other problems. Your methods are static, so they will not be able to reference non-static (instance) variables. My suggestion is to remove the "static" modifier from these methods. When you do that, you won't be able to call them from main without creating an instance. So inside of main, you should create a new instance, like...

    MultipleLoans_2 myLoans = new MultipleLoans_2();

    Then use that instance to call the methods...

    myLoans.getUserInput(...);
    myLoans.printValues(...);

    After that, your getUserInput method might throw an IOException, which needs to be caught or declared.

    Then your code will compile and run without error. (I didn't check the logic, but the output looks about right -- although you probably won't like the formatting.)
    [ March 02, 2008: Message edited by: marc weber ]
     
    what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic