If statements -- need wrapper

Tried a While statement and it didn't work.

Page 1 of 1

2 Replies - 958 Views - Last Post: 14 January 2008 - 10:27 AM Rate Topic: -----

#1 Reason4  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 20-December 07

If statements -- need wrapper

Posted 14 January 2008 - 07:31 AM

Sorry to be a wart about this but I am truly stuck and need help!

Here is another version of the program I have been working on. This one uses a series of If statements but I can't get it to loop through them until a user chooses to end the program. Also, my "end" statement doesn't appear to work.

/*********************************************************************************************
Week 5 Individual Assignment: Mortgage Payment Calculation - Change Request #3


The purpose of this program is to implement Change Request #3 to SR-mf-003, which asks that
the mortgage payment program display interest and principal payments required to fully
amortize three loans with different loan periods and different interest rates. The program
must use an array for the different loans and should display the.mortgage payment amount for
each loan and then list the loan balance and interest paid for each payment over the term of
the loan. Loops should be used to keep lists from scrolling off the screen.This change request
asks that a graphical user interface (GUI) not be used.
***********************************************************************************************/


/**********************************************************************************************
Import Utilities
***********************************************************************************************/

import java.io.*; //This calls error exception code and enables various input/output options.
import java.text.*; //This is for decimal formatting.
import java.util.*; //This offers several methods, including input aids.

/**********************************************************************************************
Identify class Week5Mortgage and set access as public
***********************************************************************************************/

public class Week5Mortgage
{//begin Class Week5Mortgage
	public static void main (String[] args) throws IOException
	{//begin main

/***********************************************************************************************
Initialize and declare variables
************************************************************************************************/

		DecimalFormat two = new DecimalFormat("$###,###.00");
		//DecmalFormat renders output in currency;
		double principal = 200000;
		int code = 0;
		boolean done = false;
		String userChoice;
		int choice;
		BufferedReader myIn= new BufferedReader(new InputStreamReader(System.in));

/***************************************************************************************************
Initialize and declare arrays
****************************************************************************************************/

		double[] term = {84, 180, 360};
		double[] rate = {.0535, .0550, .0575};
		double[] payment = {2859.72, 1634.17, 1167.15};

/***************************************************************************************************
Calculations for principal and interest payments
****************************************************************************************************/


/***************************************************************************************************
System Output
****************************************************************************************************/

System.out.println();
	   System.out.println("+==================================================+");
	   System.out.println("|  ***										***  |");
	   System.out.println("|  ***		Mortgage Payment Program		***  |");
	   System.out.println("|  ***										***  |");
	   System.out.println("+==================================================+");
	   System.out.println();

	   System.out.println("\t\t Loans Available from \n \t McBride Financial Services \n");
	   System.out.println("Loan No. \t Term \t Interest \t Payment");

	   System.out.println("\n  1	\t	   7 Yrs \t	5.35% \t" + (two.format(payment[0])));
	   System.out.println("\n  2	\t	  15 Yrs \t	5.50% \t" + (two.format(payment[1])));
	   System.out.println("\n  3	\t	  30 Yrs \t	5.75% \t" + (two.format(payment[2])));
	   System.out.println("");

/***************************************************************************************************
Request for User Input
****************************************************************************************************/

System.out.print("Enter a number between 1 and 4, then press ENTER.\n");
userChoice = myIn.readLine();
choice = Integer.parseInt(userChoice);


/***************************************************************************************************
IF statements to display loan information and exit the program
****************************************************************************************************/

if (choice == 1)
{System.out.println("You have chosen Loan No. 1\n"); //placeholder for calculations and results display
System.out.println("Loan Term \t Payment \t Monthly Payment\n");//placeholder code
System.out.println("7 Years \t  5.35% \t	" + (two.format(payment[0])));//placeholder code
System.out.println("");//placeholder code
}
//need to do calculations and display loan amortization for Loan 1


if (choice == 2)
{System.out.println("You have chosen Loan No. 2\n"); //placeholder for calculations and results display
System.out.println("Loan Term \t Payment \t Monthly Payment\n");//placeholder code
System.out.println("15 Years \t  5.50% \t	" + (two.format(payment[1])));//placeholder code
System.out.println("");//placeholder code
}
//need to do calculations and display and display loan amortization for Loan 2


if (choice == 3)
{System.out.println("You have chosen Loan No. 2\n"); //placeholder for calculations and results display
System.out.println("Loan Term \t Payment \t Monthly Payment\n");//placeholder code
System.out.println("30 Years \t  5.75% \t	" + (two.format(payment[2])));//placeholder code
System.out.println("");//placeholder code
}
//need to do calculations and display loan amortization for Loan 3


/*
if (choice == 4);

  break;
*/



  //need device for allowing user to pick another loan rather than exiting program after each choice.
  //need to fix exit program code

 } //end main

} //end Class Week5Mortgage



As stated in the last comment above, I need the program to loop through the choices until a user chooses 4, then I need an exit code that works. I have tried

System.out.println("Thank you for playing.");
		System.exit(0);



but it didn't work in the present format.

Any ideas or suggestions are most appreciated.

Folks, you say we should make an effort before asking for help. I have been working on this program intensively for the last week. Either I am just too dense to comprehend anything or am too blind to see what should be sitting in the textbook! In either case, I am fresh out of ideas! Help!!!

Is This A Good Question/Topic? 0
  • +

Replies To: If statements -- need wrapper

#2 Liontrue1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 48
  • View blog
  • Posts: 811
  • Joined: 20-May 06

Re: If statements -- need wrapper

Posted 14 January 2008 - 10:17 AM

I saw:

if (choice == 4);


this is incorrect syntax. you need to use:

i
f (choice == 4){
//Do some events...
}


If you want the program to run as long as the used does not choose 4, this code is the basis:

boolean ok = true;
while (ok == true){
	//Get user input (1 to 4) and process it
	if (choice == 1){
		//...
	}
	//...
	else if (choice == 4){
		ok = false;
	}
}
System.exit(0);


hope this helps, if u have questions just reply.
Was This Post Helpful? 0
  • +
  • -

#3 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: If statements -- need wrapper

Posted 14 January 2008 - 10:27 AM

View Postalpha02, on 14 Jan, 2008 - 06:17 PM, said:

I saw:

if (choice == 4);


this is incorrect syntax.


Not incorrect, but usually not a good practice. If it would be incorrect syntax, he would receive compiler errors. However I don't understand that break right after it, it does nothing.
Other than that alpha02's solution should work fine!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1