Please Help!! I'm out of ideas to fix my code - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Please Help!! I'm out of ideas to fix my code

JavaNewb666JavaNewb666 Posts: 3Member
I'm trying to compare 3 loans. The loan amount is hardcoded to 150,000. I'm trying to get user input for rate and term and display results. Nothing fancy but I'm really stumped. Any help is much appreciated. Here's what I have so far:

import java.io.*;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class LoanComparison
{
public static void main(String[] args) throws IOException
{
// variables
String strInterest, strMonths;
Double dblamount = 150000;

// arrays
double[] dblMonths = new double[TotalLoans]; // stores loan term in months
double[] dblInterest = new double[TotalLoans]; // stores interest rate

//print program header
System.out.println(" Loan Comparison

");
System.out.println("This program compares three mortgages with different interest rate and term");
System.out.println();

// Get information from user and place information into arrays
for (int i = 0; i < TotalLoans; i++)
{

dblInterest[i] = getInterest(i+1);
dblMonths[i] = getMonths(i+1);
}

// calculate and display
ResultCalculation(dblInterest, dblMonths);

}

// Mortgage interest from user
public static double getInterest(int intMortgageRate)
{
String strInput;
double dblInput = 0;

// Get interest rate from userl good value.
do
{
strInput = JOptionPane.showInputDialog(null, "Enter interest rate in percent for loan #" + intMortgageRate + ":
click cancel to exit");

if (strInput == null) System.exit(0); //EXIT

try //Convert to double
{
dblInput = Double.parseDouble(strInput);
if(dblInput <= 0 || dblInput > 120) throw new NumberFormatException();
}
catch (NumberFormatException nfe)
{
JOptionPane.showMessageDialog(null, "Enter a positive number.", "Error", JOptionPane.ERROR_MESSAGE);
}

}while(dblInput <= 0);

return dblInput;
}

// Get loan term in months
public static double getMonths(int intLoanTerm)
{
String strInput;
double dblInput = 0;

// Get number of months from user
do
{
strInput = JOptionPane.showInputDialog(null, "Enter Loan Term in months #" + intLoanTerm + ":
click cancel to exit");

if (strInput == null) System.exit(0);

// convert into double
try
{
dblInput = Double.parseDouble(strInput);
if(dblInput <= 0) throw new NumberFormatException();
}
catch (NumberFormatException nfe)
{
JOptionPane.showMessageDialog(null, "Enter a positive number.", "Error", JOptionPane.ERROR_MESSAGE);
}

}while(dblInput <= 0);

return dblInput;
}

// Calculations
public static void ResultCalculation(double[] dblInterest, double[] dblMonths)
{
// variables
int intNumber = 1;
String strInput;
double dblInterestDecimal =0;
double dblNewAmount = 150000;
double dblCurrentInterest =0;
double dblMonthlyPrinciple = 0;
double dblNewPrincipleAmount = 0;

// arrays
double[] dblPayment = new double[TotalLoans];
double[] dblTotalInterest = new double[TotalLoans];

// Declare new DecimalFormat to format output
DecimalFormat df = new DecimalFormat("$#,##0.00");

// Calculate loan parameters for each loan
for (int i = 0; i < TotalLoans; i++)
{
dblInterestDecimal = (dblInterest[i] / 100D)/12D;

// Calculate monthly payment.
dblPayment[i] = (dblNewAmount * Math.pow((1 + dblInterestDecimal), dblMonths[i]) * dblInterestDecimal) / ((Math.pow((1 + dblInterestDecimal), dblMonths[i])) - 1);

// Calculate total loan interest
do
{
// current monthly interest
dblCurrentInterest = dblNewAmount * dblInterestDecimal;
// Sum total interest
dblTotalInterest[i] = dblTotalInterest[i] + dblCurrentInterest;
// monthly principle
dblMonthlyPrinciple = dblPayment[i] - dblCurrentInterest;
// New Principle Amount
dblNewPrincipleAmount = dblNewAmount - dblMonthlyPrinciple;
dblNewAmount = dblNewPrincipleAmount;
intNumber++;
}while(dblNewAmount > 1);

// set variables to zero
dblInterestDecimal =0;
dblNewAmount = 0;
dblCurrentInterest = 0;
dblMonthlyPrinciple = 0;
dblNewPrincipleAmount = 0;
}

// Print header
System.out.println();
System.out.println();
System.out.println("Loan Amount Interest Monthly Total");
System.out.println("Number Loaned Rate Payment Interest");
System.out.println("________________________________________________________________________________");

// Print data
System.out.println("Loan 1 " + df.format(Amount[0]) + " " + dblInterest[0] + "% " + df.format(dblPayment[0]) + " " + df.format(dblTotalInterest[0]));
System.out.println("Loan 2 " + df.format(Amount[1]) + " " + dblInterest[1]+ "% " + df.format(dblPayment[1]) + " " + df.format(dblTotalInterest[1]));
System.out.println("Loan 3 " + df.format(Amount[2]) + " " + dblInterest[2]+ "% " + df.format(dblPayment[2]) + " " + df.format(dblTotalInterest[2]));

System.out.println();
}

}

Comments

  • codebytescodebytes Posts: 12Member
    You are almost there. Your biggest oversight was that you did not initialise TotalLoans anywhere in the program. As you use it twice, in methods main and ResultCalculation, I declared it as global and made it a constant because you are only interested in comparing three loans. You also made a couple of mistakes in ResultCalculation. When printing out the data you had an undeclared variable Amount[], which presumably you intend to be the loan amount which you hard coded as 150,000 in all three cases. I replaced this with dblNewAmount. Having calculated the loan parameters for the first loan you correctly reinitialise the variables but dblNewAmount should be reset to 150,000 not zero. Finally in the calculation of the interest (dblInterestDecimal) I divided by the term of the loan as this may not always be 12 months. There were a few other small changes as well. The debugged code is here and I've commented the changes I made :-

    import java.io.*;
    import java.text.DecimalFormat;
    import javax.swing.JOptionPane;

    public class LoanComparison
    {

    // declare total loans and mortgage amount and make global.

    private static final int TotalLoans = 3; // I made total loans a constant.


    public static void main(String[] args) throws IOException
    {
    // variables (local)
    String strInterest, strMonths;


    // arrays
    double[] dblMonths = new double[TotalLoans]; // stores loan term in months
    double[] dblInterest = new double[TotalLoans]; // stores interest rate

    //print program header
    System.out.println(" Loan Comparison

    ");
    System.out.println("This program compares three mortgages with different interest rate and term");
    System.out.println();

    // Get information from user and place information into arrays
    for (int i = 0; i < TotalLoans; i++)
    {

    dblInterest[i] = getInterest(i+1);
    dblMonths[i] = getMonths(i+1);
    }

    // calculate and display
    ResultCalculation(dblInterest, dblMonths);

    // end program
    System.exit(0);

    }

    // Mortgage interest from user
    public static double getInterest(int intMortgageRate)
    {
    String strInput;
    double dblInput = 0;

    // Get interest rate from userl good value.
    do
    {
    strInput = JOptionPane.showInputDialog(null, "Enter interest rate in percent for loan #" + intMortgageRate + ":
    click cancel to exit");

    if (strInput == null) System.exit(0); //EXIT

    try //Convert to double
    {
    dblInput = Double.parseDouble(strInput);
    if(dblInput <= 0 || dblInput > 120) throw new NumberFormatException();
    }
    catch (NumberFormatException nfe)
    {
    JOptionPane.showMessageDialog(null, "Enter a positive number.", "Error", JOptionPane.ERROR_MESSAGE);
    }

    }while(dblInput <= 0);

    return dblInput;
    }

    // Get loan term in months
    public static double getMonths(int intLoanTerm)
    {
    String strInput;
    double dblInput = 0;

    // Get number of months from user
    do
    {
    strInput = JOptionPane.showInputDialog(null, "Enter Loan Term in months for loan #" + intLoanTerm + ":
    click cancel to exit");

    if (strInput == null) System.exit(0);

    // convert into double
    try
    {
    dblInput = Double.parseDouble(strInput);
    if(dblInput <= 0) throw new NumberFormatException();
    }
    catch (NumberFormatException nfe)
    {
    JOptionPane.showMessageDialog(null, "Enter a positive number.", "Error", JOptionPane.ERROR_MESSAGE);
    }

    }while(dblInput <= 0);

    return dblInput;
    }

    // Calculations
    public static void ResultCalculation(double[] dblInterest, double[] dblMonths)
    {
    // variables
    int intNumber = 1;
    String strInput;
    double dblInterestDecimal =0;
    double dblNewAmount = 150000;
    double dblCurrentInterest =0;
    double dblMonthlyPrinciple = 0;
    double dblNewPrincipleAmount = 0;

    // arrays
    double[] dblPayment = new double[TotalLoans];
    double[] dblTotalInterest = new double[TotalLoans];

    // Declare new DecimalFormat to format output
    DecimalFormat df = new DecimalFormat("$#,##0.00");

    // Calculate loan parameters for each loan
    for (int i = 0; i < TotalLoans; i++)
    {
    dblInterestDecimal = (dblInterest[i] / 100D)/dblMonths[i]; // divide by the term !

    // Calculate monthly payment.
    dblPayment[i] = (dblNewAmount * Math.pow((1 + dblInterestDecimal), dblMonths[i]) * dblInterestDecimal) / ((Math.pow((1 + dblInterestDecimal), dblMonths[i])) - 1);

    // Calculate total loan interest
    do
    {
    // current monthly interest
    dblCurrentInterest = dblNewAmount * dblInterestDecimal;
    // Sum total interest
    dblTotalInterest[i] = dblTotalInterest[i] + dblCurrentInterest;
    // monthly principle
    dblMonthlyPrinciple = dblPayment[i] - dblCurrentInterest;
    // New Principle Amount
    dblNewPrincipleAmount = dblNewAmount - dblMonthlyPrinciple;
    dblNewAmount = dblNewPrincipleAmount;
    intNumber++;
    }while(dblNewAmount > 1);

    // set variables to zero
    dblInterestDecimal =0;
    dblNewAmount = 150000; // this should not be initialised to zero !
    dblCurrentInterest = 0;
    dblMonthlyPrinciple = 0;
    dblNewPrincipleAmount = 0;
    }

    // Print header
    System.out.println();
    System.out.println();
    System.out.println("Loan Amount Interest Monthly Total");
    System.out.println("Number Loaned Rate Payment Interest");
    System.out.println("________________________________________________________________________________");

    // Print data
    System.out.println("Loan 1 " + df.format(dblNewAmount) + " " + dblInterest[0] + "% " + df.format(dblPayment[0]) + " " + df.format(dblTotalInterest[0]));
    System.out.println("Loan 2 " + df.format(dblNewAmount) + " " + dblInterest[1]+ "% " + df.format(dblPayment[1]) + " " + df.format(dblTotalInterest[1]));
    System.out.println("Loan 3 " + df.format(dblNewAmount) + " " + dblInterest[2]+ "% " + df.format(dblPayment[2]) + " " + df.format(dblTotalInterest[2]));

    System.out.println();
    }

    }

    You should indent your code more for readability.
Sign In or Register to comment.