Hi,

I'm new to java and I have a homework.
Below is my working code, however, I would like to have a loop in order to return to the menu for user's input.

package mortgagecalc_cr2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author adr1817
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        /*************************************************************
	**	Initialze Variables                                 **
	**	Decimal Format to return 2 decimal point.           **
	**                                                          **
 	*************************************************************/
        String userChoice;
	int Choice;
        BufferedReader myIn= new BufferedReader(new InputStreamReader(System.in));  
        java.text.DecimalFormat df = new java.text.DecimalFormat("###,###.00");
             
        double Principal = 200000;
        int termOne = 84;
        int termTwo = 180;
	int termThree = 360;
        double rateOne = .0535;
        double rateTwo = .0550;
        double rateThree = .0575;

	for (int i=0; i<1; i++) {

		double firstOption, secondOption, thirdOption, currentBalance, monthlyInterestPaid;
		double mosIntrateOne = (rateOne/12);
		double mosIntrateTwo = (rateTwo/12);
		double mosIntrateThree = (rateThree/12);

                System.out.println();
		System.out.printf("%51s\n", "Mortgage Payment Calculator");
        	System.out.printf("%60s\n", "===========================================");
		System.out.println();
		System.out.println("This program will show you the monthly mortgage payment of each loan.");
		System.out.println("Please select from the three loan types.");
		System.out.println();

		System.out.printf("%10s\n", "Loan amount = $200,000\n");
		System.out.printf("%48s\n", "Loan #1:  7yrs, 5.35%");
		System.out.printf("%48s\n", "Loan #2: 15yrs, 5.50%");
		System.out.printf("%48s\n", "Loan #3: 30yrs, 5.75%");
		System.out.println();
                System.out.println("\t\tView all loans - press 4 on your keyboard.");
 		System.out.println();

        /***********************************************************************
	**	This is where it will prompt the user to do a choose	      **
	**	which loan to take.					      **
        ***********************************************************************/

		System.out.println ("Which loan would you like to take a look at?");
		System.out.println ("Please key-in a number from 1 to 4, then press ENTER: ");

		userChoice = myIn.readLine();
		Choice = Integer.parseInt(userChoice);

/* Math equation */
firstOption = (Principal * mosIntrateOne) / (1 - 1 / Math.pow((1 + mosIntrateOne), termOne));

secondOption = (Principal * mosIntrateTwo ) / (1 - 1 / Math.pow((1 + mosIntrateTwo ), termTwo));

thirdOption = (Principal * mosIntrateThree ) / (1 - 1 / Math.pow((1 + mosIntrateThree ), termThree));

currentBalance = Principal;


/*Condition */
        switch(Choice){
            case 1:
                System.out.println();
    		System.out.println();
        	System.out.println("You have chosen Loan #1\n");
                System.out.println("Loan Term \t Interest \t Monthly Payment");
                System.out.println("7 Years \t   5.35% \t    " + (df.format(firstOption)));
                break;

            case 2:
                System.out.println();
                System.out.println();
                System.out.println("You have chosen Loan #2\n");
                System.out.println("Loan Term \t Interest \t Monthly Payment");
                System.out.println("15 Years \t  5.50% \t    " + (df.format(secondOption)));
                break;

            case 3:
                System.out.println();
                System.out.println();
                System.out.println("You have chosen Loan #3\n");
                System.out.println("Loan Term \t Interest \t Monthly Payment");
                System.out.println("30 Years \t  5.75% \t    " + (df.format(thirdOption)));
                break;

            case 4:
                System.out.println();
                System.out.println("You have chosen to view all 3 loans.\n");
                System.out.println("Loan Term \t Interest \t Monthly Payment");
                System.out.println("7 Years \t   5.35% \t    " + (df.format(firstOption)));
                System.out.println("15 Years \t  5.50% \t    " + (df.format(secondOption)));
                System.out.println("30 Years \t  5.75% \t    " + (df.format(thirdOption)));
                break;

            default:
                InvalidChoice();    // Calls an invalid selection when different number is press
                }

        }
                                                                    }

    private static void InvalidChoice() {
        System.out.println("Invalid selection!");
    }

                                    }

First, please put your codes inside the CODES tag.

Based on my understanding,

1. You will ask user what type of loan they want to look at.
2. You will show the loan computation if choice is 1 - 4.
3. If invalid choice, you will display invalid choice message.

My suggestions.


1. For INVALID choice, after you display the invalid message, you may want to display the choices again so that the user will know what to pick.

2. For VALID choice, you may want to ask the user if they want to look at other computations.

Given that you follow my suggestions, you may want to do the loop here.

boolean userEnd = false;
do { // this should replace your for loop

// 1. display the necessary screens until the choice for 1 -4 

// 2. do your switch statement here.

    switch {
           // for valid choices, before you break, display something like "do you want to look at other loan computations??", then using scanner class, check if Y or N. if N then that means user wnats to exit so you set userEnd to true.
    }
}
while !userEnd;
commented: If a posting has broken any of the rules of DaniWeb,Please use "Flag Bad Post" to report the team of moderators. +11
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.