Hello all,

New to the forum and to java. Here is an assignment I have been staring at for days. When I run my program it is multiplying the the loan amount by the interest rate and spitting thaT out as the answer for the monthly payment. What am I doing wrong here? Thanks for the help!

Also, the program is supposed to kick back an error if you type anything other than y/n to continue havent figured that one yet either....

package chapt05;

import java.text.NumberFormat;
import java.util.InputMismatchException;
import java.util.Scanner;

public class CH05PR52 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// welcome the user to the program
		System.out.println("Welcome to the Loan Calculator\n");
		System.out.println(); // print a blank line

		// create a Scanner object named sc
		Scanner sc = new Scanner(System.in);
		String choice = "y";
		while (choice.equalsIgnoreCase("y"))

		{
			// get the input from the user
			System.out.println("DATA ENTRY");
			double loanAmount = getDoubleWithinRange(sc, "Enter loan amount: ",
					0, 1000000);
			double monthlyInterestRate = getDoubleWithinRange(sc,
					"Enter yearly interest rate: ", 0, 20);
			int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100);

			// Calculate the monthly payment
			int months = years * 12;
			double monthlyPayment = loanAmount * monthlyInterestRate 
			/ (1 - 1 /Math.pow(1 + monthlyInterestRate, months));
					

			// get the currency and percent formatters
			NumberFormat currency = NumberFormat.getCurrencyInstance();
			NumberFormat percent = NumberFormat.getPercentInstance();
			percent.setMinimumFractionDigits(1);

			// format the result as a single string
			String results = "Loan amount:\t" + currency.format(loanAmount)
					+ "\n" + "Yearly interest rate:\t"
					+ percent.format(monthlyInterestRate/100) + "\n"
					+ "Number of years:\t" + years + "\n"
					+ "Monthly payment:\t\t" + currency.format(monthlyPayment)
					+ "\n";

			// print the results
			System.out.println();
			System.out.println("FORMATTED RESULTS");
			System.out.println(results);

			// see if the user wants to continue
			System.out.print("Continue? (y/n): ");
			choice = sc.next();
			sc.nextLine(); // discard any other data entered on the line
			System.out.println();

		}

	}

	private static int getIntWithinRange(Scanner sc, String prompt, int min,
			int max) {
		// TODO Auto-generated method stub
		int i = 0;
		boolean isValid = false;
		while (isValid == false) {
			i = getInt(sc, prompt);
			if (i <= min)
				System.out.println("Error! Number must be greater than " + min
						+ ".");
			else if (i >= max)
				System.out.println("Error! Number must be less than " + max
						+ ".");
			else
				isValid = true;

			
		}
		return i;

	}

	private static int getInt(Scanner sc, String prompt) {
		// TODO Auto-generated method stub
		int i = 0;
		boolean isValid = false;
		while (isValid == false) {
			System.out.print(prompt);
			if (sc.hasNextInt()) {
				i = sc.nextInt();
				isValid = true;
			} else {
				System.out.println("Error! Invalid integer value. Try again.");
			}
			sc.nextLine();
		}

		return i;
	}

	private static double getDoubleWithinRange(Scanner sc, String prompt,
			int min, int max) {
		// TODO Auto-generated method stub
		double d = 0.0;
		boolean isValid = false;
		while (isValid == false) {
			d = getDouble(sc, prompt);
			if (d <= min) {
				System.out.println("Error! Number must be greater than " + min
						+ "");
			} else if (d >= max) {
				System.out.println("Error! Number must be less than " + max
						+ ".");
			} else
				isValid = true;
		}
		return d;
	}

	private static double getDouble(Scanner sc, String prompt) {
		// TODO Auto-generated method stub
		double d = 0.0;
		boolean isValid = false;
		while (isValid == false) {
			System.out.print(prompt);
			if (sc.hasNextDouble()) {
				d = sc.nextDouble();
				isValid = true;
			} else {
				System.out.println("Error! Invalid decimal value. Try again.");
			}
			sc.nextLine();
		}

		return d;
	}
}

And what is your formula for monthly payment supposed to be? I don't have a mortgage currently, and I don't know that information. But I'd think it'd be based on the total amount of time you plan on paying off the mortgage over ..

This article has been dead for over six months. Start a new discussion instead.