Hi I need help with the while loop and read from a file to find monthly mortgage payment from the input of Principle, Interest & Term in years. I know I may did something that doesn't make sense here, please take a look and let me know. Thank you in advance. Below is the errors I got when Run it in jgrasp. Tracy

import java.text.*; // format output
import java.util.*; // Scanner class
import java.io.*; // Needed for file classes

public class MortgageLoopC
{
	public static void main(String[] args)throws IOException
	{
		DecimalFormat num=new DecimalFormat("$,###.00"); // Create format for name num
		DecimalFormat interest=new DecimalFormat("%,##.00"); // Create format for name interest

		// variable names of Mortgage Principle, Interest, Monthly Payment & Term.
		double P, // Mortgage Principle		
		I, // Mortgage Interest
		T, // Mortgage Term
		MP; // Monthly Mortgage Payment
		
		String holdInput, //to hold user input.
		mortgageC, // File name
		str; // string file to be read
		
		char repeat; // to 'Y' or 'N'.
				

		// Initialize Scanner to read from DOS window.
		Scanner input = new Scanner(System.in);
		
		// Get the file name
		System.out.print("Enter the file name: ");
		mortgageC=input.nextLine();
		
		// Open the file
		FileReader freader=new FileReader("promptC.txt");
		BufferedReader inputFile=new BufferedReader(freader);
		
		// Get as many different mortgage payments as user wants
		do
		{
			// Read the first line from the file
			//"Calculates the monthly mortgage payment from your input data"
			str=inputFile.readLine();
			
			// Read the remaining prompts
			
			while(str!=null)
			{
				// Display the next prompt
				//"Enter the mortgage Principle"
				system.out.println(str);
				P=input.nextDouble();
				
				// Read the next prompt
				str=inputFile.readLine();
			}
			
			while(str!=null)
			{
				// Display the next prompt
				//"Enter the mortgage Interest"
				system.out.println(str);
				I=input.nextDouble();
				I = I/100; // format for user type in common bank rate.
				
				// Read the next prompt
				str=inputFile.readLine();
			}

			while(str!=null)
			{
				// Display the next prompt
				//"Enter the mortgage Term in year"
				system.out.println(str);
				T=input.nextDouble();
				
				// Read the next prompt
				str=inputFile.readLine();
			}		
				
			// Compute the monthly mortgage payment MP.
			MP = (P*(I/12))/(1-Math.pow((1/(1+(I/12))),(T*12)));

			// Display the monthly mortgage payment on the DOS window.
			System.out.println("Monthly Mortgage Payment of "+P+" for "+T+" years at "+interest.format(I)+" is: "+num.format(MP));

			System.out.println(); //Prints a blank line.

			// Does the user want to input a different data	set?
			System.out.println("Would you like to calculate"+"another set of Monthly Mortgage?");
			System.out.print("Enter Y for Yes or N for No: ");

			str=input.next(); // Read next char
			repeat=str.charAt(0); // Get the first char.
			
		} while (repeat=='Y' || repeat=='y');
	}
}

MortgageLoopC.java:60: package system does not exist
system.out.println(str);
^
MortgageLoopC.java:71: package system does not exist
system.out.println(str);
^
MortgageLoopC.java:83: package system does not exist
system.out.println(str);
^
3 errors

Edited 5 Years Ago by Dani: Added code tags

it's still me wondering,
How can the user can still input their data each time they read a line until all those number are calculated at the end?
Do I need to use 3 while loop or just 1 ?

Check the line numbers 60, 71 and 83.
It should be System.out.println instead of system.out.println because in java,
there is no package named system, it is System.
Correct it and then check if you are still getting problem.

1) Class names should be noun, such as MortgageCalculator

2) You need to initialize P, I, and T since their initialization currently resides inside of a loop that may or may not begin. If it doesn't begin, they will never be initialized when you try to perform calculations on them and will throw an exception. Just do "double P = 0;"

3) Use specific package names to import and not wild card operators.
import java.io.FileReader; and not import java.io.*;

4) You're going to have some infinite loops:

while(str!=null)
			{
				// Display the next prompt
				//"Enter the mortgage Principle"
				System.out.println(str);
				P=input.nextDouble();
				
				// Read the next prompt
				str=inputFile.readLine();
			}

This will run as long as str is not null, but str can never be null.

5) Does not appear that you need to import a file since you are using prompts to gather data.

6) Generally, you want to create variables where they are used and not necessarily at the top of the code. That is, unless they are global variables.

import java.text.DecimalFormat;
import java.util.Scanner;


public class MortgageCalculator {

	public static void main(String[] args) {
		
		System.out.println("Mortgage Calculator");
		System.out.println();
		
		char input;
		do {
			System.out.print("Principal        : ");
			double P = new Scanner(System.in).nextDouble() / 100;
			
			System.out.print("Interest Rate    : ");
			double I = new Scanner(System.in).nextDouble();
			
			System.out.print("Loan Term        : ");
			double T = new Scanner(System.in).nextDouble();
			
			System.out.println("Monthly Payment  : " + 
					new DecimalFormat("$, ###.00").format(
							(P * (I / 12)) / (1 - Math.pow((1 / (1 + (I / 12))), (T * 12)))));
			
			System.out.println();
			System.out.print("Again? (Y/N) ");
			input = new Scanner(System.in).next().charAt(0);
			System.out.println();
			
		} while (input != 'n' && input != 'N');
		
		System.out.println("Good-Bye!");
	}
}

Console:

Mortgage Calculator

Principal        : 100000
Interest Rate    : 5.5
Loan Term        : 30
Monthly Payment  : $458.33 

Again? (Y/N) y

Principal        : 150000
Interest Rate    : 6.5
Loan Term        : 30
Monthly Payment  : $812.50 

Again? (Y/N) n

Good-Bye!
This article has been dead for over six months. Start a new discussion instead.