9 Replies - 9101 Views - Last Post: 15 August 2010 - 06:50 PM Rate Topic: -----

#1 buniwai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 10-December 08

How to add to a exit program to an If statement

Posted 15 August 2010 - 02:14 PM

I already have a Please Press Enter to continue... Statement for the end of my list that will allow the list to stay on the screen, be read, then when the user is ready they hit enter and the list continues to the next page. That is part of the assignment what I want to add is not part of the assignment but I think would be cool to learn. I would like to give the user the option of either Pressing Enter to Continue (have this) or to return to the main console, or to exit the program.

Do I use a series of IF statements, or If/else statements, is what I am looking for called something else?

I googled what I thought to as on how to do this but I still can't find anything.

Thanks for any help you can give.

Here is code

/*---------------------------------------------------------------------------*/
//                            PROGRAM INFORMATION
//                        Programmer : Mari Dutton
//
//  Date    : (8/14/2010)
//  Name    : MortgageCalculator
//  Filename: MortgageCalculator.java
//	Purpose : The purpose of this program is to calculate mortgage payments.
//			  To calculate and display the customer's monthly mortgage
//            of $200000 at 5.75% interest over 30 years.The program will display
// 			  360 payments with interest by the Month for 30 years.

//---------------------------------------------------------------------------*/

        import java.text.DecimalFormat;     			//to import decimal format for money
	    import java.text.NumberFormat;	    			//to import number format
		import java.io.Console;        					//Calls the class Console for user input and output


    public class MortgageCalculator1	    				// Starts the Class
   {
       public static void main(String[]args) throws Exception			//declaring main method with thrown Exception
      {

		//Arrays
		double   loanAmount = 200000;       		//Starting Balance of loan
		double[] tMonths = {360,180,84};	  		// Array for year term of the Loan
		double[] iRate   = {.0575,.055,.0535};  	// Array for interest rate of the Loan

	    //Varibles
 		double balance;                            //Final balance of each month
	    double principal;						   // Principal of each month
	    double monthlyInterestPaid;                // Monthly Interest paid with each payment

		//Table display
		int row;
		int maxRows = 12;  //maximum number of rows to display

        //User entry input
        int userEntry;
        String userEntry2;
		String userEntry3;
	    //Varibles for calculations per loan type

		double mPayments30;
		double mPayments15;
		double mPayments07;

//calculating the arrays
mPayments30 = ((iRate[0]/12) * loanAmount) / (1-(Math.pow(1+(iRate[0]/12), (- tMonths[0]))));
mPayments15 = ((iRate[1]/12) * loanAmount) / (1-(Math.pow(1+(iRate[1]/12), (- tMonths[1]))));
mPayments07 = ((iRate[2]/12) * loanAmount) / (1-(Math.pow(1+(iRate[2]/12), (- tMonths[2]))));


		//initializing balance varible
         balance = loanAmount;

		//Calls in the console
         Console c = System.console();

	//Programming infromation
	System.out.println();
		System.out.println("\t\t Mortgage Calculator");
		System.out.println("\t\t -------------------");
		System.out.println("\t\t Programmed by Mari Dutton");
		System.out.println("\t\t Mobile Loan Calculator   ");
		System.out.println("");


	//User imput/instance Menu to determine type of loan
	System.out.println("");
		System.out.println("");
		System.out.println("\t\t Select your Loan. ");
		System.out.println("\t\t ------------------");
		System.out.println("\t\t 1. 30 year with interest at 5.75%.");
		System.out.println("\t\t 2. 15 year with interest at 5.5%  ");
		System.out.println("\t\t 3. 07 year with interest at 5.35% ");
		System.out.println("\t\t 4. Exit");
		System.out.println("");

	//Uner Entry Input from user
	userEntry = (int)System.in.read();


	if (c == null)
	{
            System.err.println("Console Error.");
            System.exit(5);

        }

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\\



	//=30 year loan
	//If statement to enter User Entry input
	if (userEntry == '1')
	{

System.out.println("");
		System.out.println("");
		System.out.println("Loan Information:       ");
		System.out.println("========================");
		System.out.println("Loan Amount: $200,000.00");
		System.out.println("Months: " + tMonths[0]);
		System.out.println("Interest% Rate: " + iRate[0]);
		System.out.printf ("Monthly payment amount will be $%.2f", mPayments30);
				System.out.println("");
				System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
				System.out.println("Payment          Amount         Interest Paid   Principal        Balance   ");
				System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

				for ( row =1; row <= tMonths[0]; row++)
				{

	//Calculating the Monthly Paid Interest;
	monthlyInterestPaid = (iRate[0]/12) * balance;  //calculates monthly Interest Paid


	//Calculating the Principal
        principal = mPayments15 - monthlyInterestPaid;	//calculates Principal


	//Calculating the Balance
	balance = balance - principal;


	System.out.println("Payment "  +row+
		":\t"+NumberFormat.getCurrencyInstance().format (mPayments30) +
	         ",\t"+NumberFormat.getCurrencyInstance().format(monthlyInterestPaid) +
	         ",\t"+NumberFormat.getCurrencyInstance().format(principal) +
		",\t"+NumberFormat.getCurrencyInstance().format(balance));

				if ((row % maxRows) == 0)
            		userEntry2 = c.readLine("Press Enter to continue... ");



		}
}

{

	if (userEntry == '2')
	{

		System.out.println("");
		System.out.println("");
		System.out.println("Loan Information:       ");
		System.out.println("========================");
		System.out.println("Loan Amount: $200,000.00");
		System.out.println("Months: " + tMonths[1]);
		System.out.println("Interest% Rate: " + iRate[1]);
		System.out.printf ("Monthly payment amount will be $%.2f", mPayments15);
				System.out.println("");
				System.out.println("");
				System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
				System.out.println("Payment          Amount         Interest Paid   Principal        Balance   ");
				System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

				for ( row =1; row <= tMonths[1]; row++)
				{

			//Calculating the Monthly Paid Interest;
	monthlyInterestPaid = (iRate[1]/12) * balance;  //calculates monthly Interest Paid


			//Calculating the Principal
	principal = mPayments15 - monthlyInterestPaid;	//calculates Principal


			//Calculating the Balance
			balance = balance - principal;


	System.out.println("Payment"  +row+
	  ": \t"+NumberFormat.getCurrencyInstance().format (mPayments15) +
           ", \t"+NumberFormat.getCurrencyInstance().format(monthlyInterestPaid) +
	", \t"+NumberFormat.getCurrencyInstance().format(principal) +
         ", \t"+NumberFormat.getCurrencyInstance().format(balance));

				if ((row % maxRows) == 0)
            		userEntry2 = c.readLine("Press Enter to continue... ");

		}
}
{

	if (userEntry == '3')
	{
		System.out.println("");
		System.out.println("");
		System.out.println("Loan Information:       ");
		System.out.println("------------------------");
	         System.out.println("Loan Amount: $200,000.00");
		System.out.println("Months: " + tMonths[2]);
		System.out.println("Interest% Rate: " + iRate[2]);
		System.out.printf("Monthly payment amount will be $%.2f", mPayments07);
				System.out.println("");
				System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
				System.out.println("Payment          Amount         Interest Paid   Principal        Balance   ");
				System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

				for ( row =1; row <= tMonths[2]; row++)
				{

		//Calculating the Monthly Paid Interest;
	 monthlyInterestPaid = (iRate[2]/12) * balance;  //calculates monthly Interest Paid


			//Calculating the Principal
	principal = mPayments07 - monthlyInterestPaid;	//calculates Principal


			//Calculating the Balance
			balance = balance - principal;


		System.out.println("Payment " +row+
		": \t"+NumberFormat.getCurrencyInstance().format (mPayments07) +
	         ", \t"+NumberFormat.getCurrencyInstance().format(monthlyInterestPaid) +
		    ", \t"+NumberFormat.getCurrencyInstance().format(principal) +
		     ", \t"+NumberFormat.getCurrencyInstance().format(balance));

				if ((row % maxRows) == 0)
            		userEntry2 = c.readLine("Thank you Please Press enter to exit ");
}
}

			{
			if(userEntry == '4')
				{
		System.out.println("\t Exiting the The Mortgage Calculator now. Thank you");
		System.exit(0); //calling exit method
	}
}
}
}
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: How to add to a exit program to an If statement

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: How to add to a exit program to an If statement

Posted 15 August 2010 - 02:40 PM

Moved to Java Help, as this isn't a Java EE topic.

You could use if and else-if statements, as well as a switch block.
Was This Post Helpful? 0
  • +
  • -

#3 buniwai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 10-December 08

Re: How to add to a exit program to an If statement

Posted 15 August 2010 - 02:57 PM

Shoot I thought I had posted in the right place this time.

I will look up switch blocks and see if I can figure out how to do an else-if the way I want it.

Any other suggestion or just how to word what I want to look for a tutorial.

Thanks,
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: How to add to a exit program to an If statement

Posted 15 August 2010 - 04:25 PM

There is a switch tutorial by pdkharkar and a lot of the beginner tutorials in the Java tutorials sections cover both if and switch statements.
Was This Post Helpful? 0
  • +
  • -

#5 buniwai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 10-December 08

Re: How to add to a exit program to an If statement

Posted 15 August 2010 - 05:06 PM

View Postbuniwai, on 15 August 2010 - 01:57 PM, said:

Shoot I thought I had posted in the right place this time.

I will look up switch blocks and see if I can figure out how to do an else-if the way I want it.

Any other suggestion or just how to word what I want to look for a tutorial.

Thanks,

I thought there had to be I use the tutorials all the time, but I just had a hard time finding that one thanks for the link.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: How to add to a exit program to an If statement

Posted 15 August 2010 - 05:20 PM

based to your post question

if(condition) {
   System.exit(0);
}


Was This Post Helpful? 0
  • +
  • -

#7 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: How to add to a exit program to an If statement

Posted 15 August 2010 - 05:30 PM

Basically you want to loop through and use a switch:
Scanner in = new Scanner(System.in);
while(true)
{
  System.out.println(print your menu or whatever);
  // assign key values to each menu command
  int key = Integer.parseInt(in.nextLine().trim());
  switch(key)
  {
    case 0:
      // whatever
      break;
    case 1:
      // whatever
      break;
    case 2:
      System.exit(0);
      break;
    default:
      System.out.println("Invalid input!");
      break;
  }
}


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: How to add to a exit program to an If statement

Posted 15 August 2010 - 06:44 PM

Wasn't the original post about an If statement not a Switch statement ?
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: How to add to a exit program to an If statement

Posted 15 August 2010 - 06:46 PM

I think the OP was about if statements and viable alternatives.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: How to add to a exit program to an If statement

Posted 15 August 2010 - 06:50 PM

switch() or not... if you are going to System.exit(0) better do it before any other alternative(s)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1