wmivey 0 Newbie Poster

I am trying to get more control on user input. I have written a couple of Exceptions to handle two different user inputs, but I want to program to retry it if it is invalid and allow the program to continue. I have tried using a labeled break and it did not work out right. I will post the source code below and made the sections in question in bold.

// wmivey
// MortgageProgram


import java.text.*;                 // Needed for NumberFormat
import java.io.*;                   // Needed for user input


class MortgageProgram {


public static void main(String[] arguments) throws IOException {


/* All static variables are located at the end of the main as private static
final variables. */


/* Pay calculations are figured within the static final variable for MonthlyPayment[] at the end
of the main. */
System.out.println("\nThe following are three possible results on a home costing "
+ nf.format(InitialLoanAmount) + ".\n\n"
+ "Mortgage\t\tTerm\t\tInterest Rate\t\tMonthly Payment\n\n"
+ "Mortgage 1\t\t" + LoanTerm[0] + " years\t\t" + (AnnualInterestRate[0] * 100)
+ " %\t\t\t" + nf.format(MonthlyPayment[0]) + "\n\n"
+ "Mortgage 2\t\t" + LoanTerm[1] + " years\t" + (AnnualInterestRate[1] * 100)
+ " %\t\t\t" + nf.format(MonthlyPayment[1]) + "\n\n"
+ "Mortgage 3\t\t" + LoanTerm[2] + " years\t" + (AnnualInterestRate[2] * 100)
+ " %\t\t\t" + nf.format(MonthlyPayment[2]) + "\n\n");


System.out.println("Please select one of these three mortgages to see an amortization list.\n\n"
+ "Type a \"1\" for Mortgage 1, a \"2\" for Mortgage 2, or a \"3\" for Mortgage 3.\n");


/* The next three variables are used as transitional variables which are assigned different
values according to which array position it is pulled from in the first user's input. */
double SelectedMonthlyPayment = 0.0;
int SelectedLoanTerm = 0;
double SelectedInterestRate = 0.0;


/* Gets first input from the user and changes it from a String into an Integer. Then the following
Switch Statement is used to decide which course of action to take from there as well as perform
an error handling function if the user's input is not a 1, 2, or a 3. Then the 'catch' method
will also act as another error handling function to determine if a valid number was entered by
the user. */
try {
// Prompt the user
System.out.print( "Enter a mortgage number from the list above: " );
// Gets user input and converts to integer
int input1 = Integer.parseInt( STDIN.readLine() );


switch (input1) {
case 1:  System.out.println("\nYou have selected Mortgage " + input1 + ".\n" );
SelectedMonthlyPayment = MonthlyPayment[0];
SelectedLoanTerm = LoanTerm[0];
SelectedInterestRate = AnnualInterestRate[0];
System.out.println("If these payments are made on a monthly basis,\n"
+ "your amortization schedule will look as follows:\n");
System.out.println("\nPayment\t\t\tLoan Balance\tPrinciple Paid\tInterest Paid\n");
pause1();
break;


case 2:  System.out.println("\nYou have selected Mortgage " + input1 + ".\n"  );
SelectedMonthlyPayment = MonthlyPayment[1];
SelectedLoanTerm = LoanTerm[1];
SelectedInterestRate = AnnualInterestRate[1];
System.out.println("If these payments are made on a monthly basis,\n"
+ "your amortization schedule will look as follows:\n");
System.out.println("\nPayment\t\t\tLoan Balance\tPrinciple Paid\tInterest Paid\n");
pause1();
break;


case 3:  System.out.println("\nYou have selected Mortgage " + input1 + ".\n" );
SelectedMonthlyPayment = MonthlyPayment[2];
SelectedLoanTerm = LoanTerm[2];
SelectedInterestRate = AnnualInterestRate[2];
System.out.println("If these payments are made on a monthly basis,\n"
+ "your amortization schedule will look as follows:\n");
System.out.println("\nPayment\t\t\tLoan Balance\tPrinciple Paid\tInterest Paid\n");
pause1();
break;


                default:  System.out.println("\nI am sorry, this is not a valid Mortgage seletion."
+ " Please try again.\n" );
System.exit(1);
}  // End of switch


}  // End of try


// Tests user input to be an integer and prints error message if it is not        catch ( NumberFormatException e ) {
System.out.println("\nI am sorry, this is not a valid Mortgage seletion."
+ " Please try again.\n" );
System.exit(1);
}  // End of catch


// Declares and initializes variables used for the amortization list
int PaymentNumber = 1;
double InterestRate = 0.0;
double InterestPayment = 0.0;
double PrinciplePayment = 0.0;
double LoanBalance = InitialLoanAmount;


// while loop to control amortization loop environment and keep it true
while (true) {
/* if for some reason the loop fails to quit this if loop will break
out of the while loop when LoanBalance is ZERO */
if (LoanBalance <= 0) {
break;
}


// Loop for amortization list
for (int count = 1; PaymentNumber <= (SelectedLoanTerm * 12) ; count++) {


// Calculations to determine amortization
InterestRate = SelectedInterestRate / 12;
InterestPayment = LoanBalance * InterestRate;
PrinciplePayment = SelectedMonthlyPayment - (LoanBalance * InterestRate);
LoanBalance = LoanBalance - PrinciplePayment;


// This is to reduce final LoanBalace figure if <= $5.00 and help break out of the loop
if (LoanBalance <= 5.00) {
LoanBalance = 0.00;
}


System.out.println(+PaymentNumber + ". " + nf.format(SelectedMonthlyPayment)
+ "\t\t" + nf.format(LoanBalance)
+ "    \t" + nf.format(PrinciplePayment) + "\t\t"
+ nf.format(InterestPayment) + "\n");
PaymentNumber++;


// Progresses 10 lines of the amortization after a brief pause
if (count == 10) {
// Prompts user for input.
System.out.println("Enter a \"C\" to CONTINUE or a \"Q\" to QUIT.");


// Initializes input2 (the second user's input) as an empty String.
String input2 = null;


/* Gets user's input, returns it to UPPERCASE, and tests it to see if
it is a 'C', a 'Q', or a bogus input. */
try {
input2 = STDIN.readLine();
input2 = input2.toUpperCase();


if (input2.equals(S1)) {
break;
}                        else if (input2.equals(S2)) {
System.exit(0);
}
else {
System.exit(0);
}
} catch (IOException ioe) {
System.out.println("IO error!");
System.exit(1);
}


count = 1;


}  // End of 'if' loop


}  // End of 'for' loop


}  // End of 'while' loop



}  // End of main


// Declares STATIC FINAL variables used within the MAIN
private static final float InitialLoanAmount = 200000;
private static final int LoanTerm[] = { 7, 15, 30 };
private static final float AnnualInterestRate[] = { 0.0535f, 0.055f, 0.0575f };
private static final double MonthlyPayment[] = { (InitialLoanAmount * (AnnualInterestRate[0]/12)) / (1 - (Math.pow
(1/(1 + (AnnualInterestRate[0]/12)), (LoanTerm[0] * 12)))) , (InitialLoanAmount * (AnnualInterestRate[1]/12)) / (1 - (Math.pow
(1/(1 + (AnnualInterestRate[1]/12)), (LoanTerm[1] * 12)))) , (InitialLoanAmount * (AnnualInterestRate[2]/12)) / (1 - (Math.pow
(1/(1 + (AnnualInterestRate[2]/12)), (LoanTerm[2] * 12)))) };
private static NumberFormat nf = NumberFormat.getCurrencyInstance();
private static BufferedReader STDIN = new BufferedReader( new InputStreamReader( System.in ) );
private static final String S1 = "C";
private static final String S2 = "Q";


/* Below are two independent pausing 'void' functions allowing for pausing intervals
according to the value set in the 'Thread.sleep()' parenthesis. */


// First delay function for user to read screen
private static void pause1() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {}
}  // End of pause1


}  // End of program
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.