else if statement

Mortgage Calculator CR # 6

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 2260 Views - Last Post: 21 March 2009 - 12:47 PM Rate Topic: -----

#1 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

else if statement

Post icon  Posted 19 March 2009 - 03:54 PM

I have two questions.

Thank you for all your help. Last week I didn't do so hot. The instructor only gave me eight out of 14 points. I felt like I really busted my hump and put alot of effort into my program and I still didn't do well.

This week I'm still struggling with the same problem. I am trying to use a drop down box and plug it into a formula by using differnt loan types. I keep getting a compile error.

MortgageCalculatorCR62.java:217: 'else' without 'if'
else if (Lables == "7 years at 5.35%")
^
1 error

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

I also need to display all the monthly payments in a jTextArea, but it is only displaying one payment.

I really do not want to sound like a whiner, because I'm trying my butt off. To put things in perspective my wife went coo koo about ten months ago and left me and puts the kids in the middle of it all. Now I'm struggling just to function.

I don't feel that the instructor is really teaching us how to program. It's about bull. Write a program, and gives you nothing to go on.

I don't want to be a programmer, I just want to finish school.

Thank you for listening. Prayers for my family are helpful too.



import java.awt.*;
import java.awt.event.*;

import java.util.*;

import javax.swing.*;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import java.text.NumberFormat;
import java.math.*;

import java.awt.BorderLayout;
import java.awt.Color;

import java.awt.Container;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JCheckBox;
import javax.swing.JFrame;

import javax.swing.JPanel;
import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.JComboBox;
import javax.swing.JFrame;





public class MortgageCalculatorCR62 {

double amount;
double term;
double payments;
double amortize;
double interestRateMonths;
double monthlyInterestPayment;
double monthlyPrincipalPayment;
int termMonths;
double balance;



JLabel amountLbl, termLbl, IntrateLbl, paymentsLbl, amortizeLb1;
JTextField amountTf, termTf, intrateTf, paymentsTf, amortizeTf;
JButton calcBtn;
JButton clearButton;
JButton exitButton;
JFrame frame;


 public MortgageCalculatorCR62() {
  System.out.println("Mortgage Calculator Change Request #6.");



// public static void MortgageCalculatorCR62(String args[]) {
   
	 String labels[] = { "7 Years at 5.35%", "15 years at 5.5%", "30 years at 5.75%",};
  

amountLbl = new JLabel("  Loan Amount In Whole Dollars");
termLbl = new JLabel("  Terms In Years");
IntrateLbl = new JLabel("  Interest Rate - Decimal Format (0.000)");
paymentsLbl = new JLabel("  Monthly Payment Amount");

amountTf = new JTextField();
JComboBox comboBox1 = new JComboBox(labels);

termTf = new JTextField();
intrateTf = new JTextField();

amortizeTf = new JTextField();
amortizeLb1 = new JLabel("Amortization");
paymentsTf = new JTextField();
calcBtn = new JButton("Calculate");
clearButton = new JButton("Clear");
exitButton = new JButton("Exit");




frame = new JFrame("Mortgage Calculator CR#6");
frame.setLayout(new GridLayout(16,16));
frame.add(amountLbl);
frame.add(amountTf);

//Added JcomboBox
frame.add(comboBox1);

/* Remove these frames when action listener is working */
  frame.add(termLbl);
  frame.add(termTf);
  frame.add(IntrateLbl);
  frame.add(intrateTf);
  
  
  
frame.add(paymentsLbl);
frame.add(paymentsTf);


//  Added amortize textfield and label
frame.add(amortizeLb1);
frame.add(amortizeTf);

frame.add(calcBtn);
frame.add(clearButton);
frame.add(exitButton);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clearButton.addActionListener(new Handler());
calcBtn.addActionListener(new Handler());
exitButton.addActionListener(new Handler());
frame.setLocation(300, 300);
frame.pack();
frame.setVisible(true);
}
private class Handler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{


Object command = event.getSource();
if(command == calcBtn)
		{
		  calculate();
		  }
if(command == clearButton)
		{

		  amountTf.setText(null);
		  termTf.setText(null);
		  intrateTf.setText(null);
		  paymentsTf.setText(null);
			 
			}

			if(command == exitButton)
			{
		  System.exit(0);
		}

}
}


  public static void main(String[] args) {
  new MortgageCalculatorCR62();
}

private void calculate() {
DecimalFormat decimalPlaces=new DecimalFormat("0,000.00");

double amount = Double.parseDouble(amountTf.getText());
double term = Double.parseDouble(termTf.getText());
double interest = Double.parseDouble(intrateTf.getText());
double interestRateMonths = interest/12;


/* Next two lines commented out to accomadate formula in else if */
//  double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
//	paymentsTf.setText("" + decimalPlaces.format(payments));

double amortize = amount - payments;
amortizeTf.setText("" + decimalPlaces.format(amortize));

/*  This part of the code is for selecting the loan amount and plugging them into the formula. */

/**/
//exit application when user presses the exit button
		Object source = e.getSource();
		if (source == exitButton)
		{
			System.exit(0);
		}
//calculate payment amount when user presses submit
  else if (source == calcBtn)
		{
			String Lables = cOption.getSelectedItem();

  else if (Lables == "7 years at 5.35%")
			{			
				double amount = Double.parseDouble(txtAmount.getText());			
				int	years = 7;
				int	months = (years * 12);
				double	interest	= .0535;		
			
			double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

				paymentsTf.setText("" + decimalPlaces.format(payments));

			}			
	else if (Lables == "15 years at 5.5%")
			{			
				double amount = Double.parseDouble(txtAmount.getText());			
				int	years = 15;
				int	months = (years * 12);
				double	interest	= .055;		
		double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

			paymentsTf.setText("" + decimalPlaces.format(payments));
			}
			else if (Lables == "30 years at 5.75%")
			{			
				double amount = Double.parseDouble(txtAmount.getText());			
				int	years = 30;
				int	months = (years * 12);
				double	interest	= .0575;		
		double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
		 paymentsTf.setText("" + decimalPlaces.format(payments));
			
		}
		
	}

/**/



/* This is the area for the amortization process */

 // displays monthly mortgage payment resulting from above calculation
		System.out.println("\n\nBased on the above criteria, "  +
		 "your monthly payment will be: " +
		  "$" + decimalPlaces.format(payments));

	// formula(s) to calculate monthly interest and principal payments
		monthlyInterestPayment = (amount * interestRateMonths);
		monthlyPrincipalPayment = (payments - monthlyInterestPayment);

   
	// start while loop  */
  while (termMonths > 0);
	 
	 
  //  {
  
  
	// information to display
	System.out.println(termMonths + "\t\t$" + decimalPlaces.format(monthlyPrincipalPayment) +
		"\t\t$" + decimalPlaces.format(monthlyInterestPayment) +
		"\t\t$" + decimalPlaces.format(amount)); 

	// decrement months
		termMonths--;

	// calculate interest and principal payments
	monthlyInterestPayment = (amount * interestRateMonths);
	monthlyPrincipalPayment = (payments - monthlyInterestPayment);
	balance = (payments - monthlyPrincipalPayment);
	 
	 /* Put amortization into a Jtextarea */

}
}



Is This A Good Question/Topic? 0
  • +

Replies To: else if statement

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: else if statement

Posted 19 March 2009 - 03:57 PM

That compile error is because you never closed off the else-if before it.

By the way...when you compare Strings for equality, you need to use the equals() method.

if (Lables.equals("7 years at 5.35%"))

This post has been edited by Locke: 19 March 2009 - 03:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: else if statement

Posted 19 March 2009 - 04:52 PM

View PostLocke, on 19 Mar, 2009 - 02:57 PM, said:

That compile error is because you never closed off the else-if before it.

By the way...when you compare Strings for equality, you need to use the equals() method.

if (Lables.equals("7 years at 5.35%"))


I believe I found where the } was missing but then I get 37 errors after that.

I put the end bracket here.
amortizeTf.setText("" + decimalPlaces.format(amortize));

}


Man I'm so confused... I seems like I had this partially working earlier today. I'm ready to bang my head off the monitor.
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: else if statement

Posted 19 March 2009 - 05:25 PM

Hm...actually...

On the problem line before...eliminate the else and see what happens.

Undo the brace change that you made because of my first post. I believe that will make it work.


WOW...had an error on my post...unmatched BBCode...GAH. Gotta redo it all now. But I'll do it again! :D

OK...2nd time, I STILL had a BBCODE error...DANGIT. I will eventually help you... :lol:

Ok, so you need to get rid of everything in the calculate() method that relates to getting an event source. You don't need those at all.

private void calculate()
{
    Object source = e.getSource();
    if (source == exitButton)

    // you don't need to test for those.

    // start with this next line.

    String Labels = (String) cOption.getSelectedItem(); // gotta typecast here!!!
    
    if (Labels == "7 years at 5.35%") // this is wrong, but I'll get to that
    {
        // ...
    }
}


Second...cOption doesn't exist. I think you meant comboBox1, but that only exists in the constructor. So make an instance variable cOption that is a JComboBox, so that you can access it later. Be sure to change this JComboBox comboBox1 = new JComboBox(labels); to however it needs to be.

Third...when comparing Strings, you HAVE to use the equals() method. if (Labels.equals("7 years at 5.35%"))

Fourth...txtAmount doesn't exist. You meant amountTf. Change those accordingly.

Fifth...all of those variables in the if statements have been declared before. I don't know what you mean to do with those variables, and I don't know if you actually want the same variables. I'm sure you'll figure that one out. :D :^:

Hope this helps! :)

This post has been edited by Locke: 19 March 2009 - 06:03 PM

Was This Post Helpful? 0
  • +
  • -

#5 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: else if statement

Posted 20 March 2009 - 05:12 AM

Oh, I'm just digging a deeper hole. Locke I tried to follow what you said, but my brain is just not working.

/* Mortgage Calculator Change Request #6 in Service Request SR-mf-003. Insert comments in the program to
document the program. Attach a design flow chart to the source code of the program.

Write the program in Java (with a graphical user interface) so that it will allow the user to select which
way they want to calculate a mortgage: by input of the amount of the mortgage, the term of the mortgage,
and the interest rate of the mortgage payment or by input of the amount of a mortgage and then select from
a menu of mortgage loans: 7 year at 5.35%, 15 year at 5.5 %, and 30 year at 5.75%. In either case, display
the mortgage payment amount and then, list the loan balance and interest paid for each payment over the term
of the loan. Allow the user to loop back and enter a new amount and make a new selection, or quit. Insert
comments in the program to document the program.

Author Phil Grabowski

University of Phoenix

PRG421 Java Programming II

Course Date 2/24/09  End Date 3/30/09

Instructor Yining Li  */
 

import java.awt.*;
import java.awt.event.*;

import java.util.*;

import javax.swing.*;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import java.text.NumberFormat;
import java.math.*;

import java.awt.BorderLayout;
import java.awt.Color;

import java.awt.Container;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JCheckBox;
import javax.swing.JFrame;

import javax.swing.JPanel;
import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.JComboBox;
import javax.swing.JFrame;





public class MortgageCalculatorCR62 {

double amount;
double term;
double payments;
double amortize;
double interestRateMonths;
double monthlyInterestPayment;
double monthlyPrincipalPayment;
int termMonths;
double balance;



JLabel amountLbl, termLbl, IntrateLbl, paymentsLbl, amortizeLb1;
JTextField amountTf, termTf, intrateTf, paymentsTf, amortizeTf;
JButton calcBtn;
JButton clearButton;
JButton exitButton;
JFrame frame;


 public MortgageCalculatorCR62() {
  System.out.println("Mortgage Calculator Change Request #6.");



// public static void MortgageCalculatorCR62(String args[]) {
   
	 String labels[] = { "7 Years at 5.35%", "15 years at 5.5%", "30 years at 5.75%",};
  

amountLbl = new JLabel("  Loan Amount In Whole Dollars");
termLbl = new JLabel("  Terms In Years");
IntrateLbl = new JLabel("  Interest Rate - Decimal Format (0.000)");
paymentsLbl = new JLabel("  Monthly Payment Amount");

amountTf = new JTextField();
JComboBox comboBox1 = new JComboBox(labels);

termTf = new JTextField();
intrateTf = new JTextField();

amortizeTf = new JTextField();
amortizeLb1 = new JLabel("Amortization");
paymentsTf = new JTextField();
calcBtn = new JButton("Calculate");
clearButton = new JButton("Clear");
exitButton = new JButton("Exit");




frame = new JFrame("Mortgage Calculator CR#6");
frame.setLayout(new GridLayout(16,16));
frame.add(amountLbl);
frame.add(amountTf);

//Added JcomboBox
frame.add(comboBox1);

/* Remove these frames when action listener is working */
  frame.add(termLbl);
  frame.add(termTf);
  frame.add(IntrateLbl);
  frame.add(intrateTf);
  
  
  
frame.add(paymentsLbl);
frame.add(paymentsTf);


//  Added amortize textfield and label
frame.add(amortizeLb1);
frame.add(amortizeTf);

frame.add(calcBtn);
frame.add(clearButton);
frame.add(exitButton);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clearButton.addActionListener(new Handler());
calcBtn.addActionListener(new Handler());
exitButton.addActionListener(new Handler());
frame.setLocation(300, 300);
frame.pack();
frame.setVisible(true);
}
private class Handler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{


Object command = event.getSource();
if(command == calcBtn)
		{
		  calculate();
		  }
if(command == clearButton)
		{

		  amountTf.setText(null);
		  termTf.setText(null);
		  intrateTf.setText(null);
		  paymentsTf.setText(null);
			 amortizeTf.setText(null);
			 
			}

			if(command == exitButton)
			{
		  System.exit(0);
			 		 
	   
}
}
}

  public static void main(String[] args) {
  new MortgageCalculatorCR62();
}

private void calculate() {
DecimalFormat decimalPlaces=new DecimalFormat("0,000.00");

double amount = Double.parseDouble(amountTf.getText());
double term = Double.parseDouble(termTf.getText());
double interest = Double.parseDouble(intrateTf.getText());
double interestRateMonths = interest/12;


/* Next two lines commented out to accomadate formula in else if */
  double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
	paymentsTf.setText("" + decimalPlaces.format(payments));

double amortize = amount - payments;
amortizeTf.setText("" + decimalPlaces.format(amortize));



/*  This part of the code is for selecting the loan amount and plugging them into the formula. */

 
//exit application when user presses the exit button
//		Object source = e.getSource();
//		if (source == exitButton)
//		{
//			System.exit(0);
//		}
//calculate payment amount when user presses submit

//  else if (source == calcBtn)
//		{
	//		String Lables = cOption.getSelectedItem();

String Labels = (String) cOption.getSelectedItem(); // gotta typecast here!!!
	
	if (Labels == "7 years at 5.35%") // this is wrong, but I'll get to that


//  else if (Lables == "7 years at 5.35%")
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 7;
				int	months = (years * 12);
				double	interest	= .0535;		
			
			double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

				paymentsTf.setText("" + decimalPlaces.format(payments));

			}			
	else if (Lables == "15 years at 5.5%")
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 15;
				int	months = (years * 12);
				double	interest	= .055;		
		double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

			paymentsTf.setText("" + decimalPlaces.format(payments));
			}
			else if (Lables == "30 years at 5.75%")
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 30;
				int	months = (years * 12);
				double	interest	= .0575;		
		double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
		 paymentsTf.setText("" + decimalPlaces.format(payments));
			
		}
		
	





/* This is the area for the amortization process */

/*

 // displays monthly mortgage payment resulting from above calculation
		System.out.println("\n\nBased on the above criteria, "  +
		 "your monthly payment will be: " +
		  "$" + decimalPlaces.format(payments));

	// formula(s) to calculate monthly interest and principal payments
		monthlyInterestPayment = (amount * interestRateMonths);
		monthlyPrincipalPayment = (payments - monthlyInterestPayment);

   
	// start while loop  
  while (termMonths > 0);
	 
	 
  //  {
  
  
	// information to display
	System.out.println(termMonths + "\t\t$" + decimalPlaces.format(monthlyPrincipalPayment) +
		"\t\t$" + decimalPlaces.format(monthlyInterestPayment) +
		"\t\t$" + decimalPlaces.format(amount)); 

	// decrement months
		termMonths--;

	// calculate interest and principal payments
	monthlyInterestPayment = (amount * interestRateMonths);
	monthlyPrincipalPayment = (payments - monthlyInterestPayment);
	balance = (payments - monthlyPrincipalPayment);
	 
	 System.out.println(balance);

*/
	 
	 /* Put amortization into a Jtextarea */

}
}


Was This Post Helpful? 0
  • +
  • -

#6 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: else if statement

Posted 20 March 2009 - 05:33 AM

Ok,

I got it down to six errors.

Typecasting, yes covert to an interger. I'm getting an error there.


/* Mortgage Calculator Change Request #6 in Service Request SR-mf-003. Insert comments in the program to
document the program. Attach a design flow chart to the source code of the program.

Write the program in Java (with a graphical user interface) so that it will allow the user to select which
way they want to calculate a mortgage: by input of the amount of the mortgage, the term of the mortgage,
and the interest rate of the mortgage payment or by input of the amount of a mortgage and then select from
a menu of mortgage loans: 7 year at 5.35%, 15 year at 5.5 %, and 30 year at 5.75%. In either case, display
the mortgage payment amount and then, list the loan balance and interest paid for each payment over the term
of the loan. Allow the user to loop back and enter a new amount and make a new selection, or quit. Insert
comments in the program to document the program.

Author Phil Grabowski

University of Phoenix

PRG421 Java Programming II

Course Date 2/24/09  End Date 3/30/09

Instructor Yining Li  */
 

import java.awt.*;
import java.awt.event.*;

import java.util.*;

import javax.swing.*;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import java.text.NumberFormat;
import java.math.*;

import java.awt.BorderLayout;
import java.awt.Color;

import java.awt.Container;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JCheckBox;
import javax.swing.JFrame;

import javax.swing.JPanel;
import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.JComboBox;
import javax.swing.JFrame;





public class MortgageCalculatorCR62 {

double amount;
double term;
double payments;
double amortize;
double interestRateMonths;
double monthlyInterestPayment;
double monthlyPrincipalPayment;
int termMonths;
double balance;
int Labels;



JLabel amountLbl, termLbl, IntrateLbl, paymentsLbl, amortizeLb1;
JTextField amountTf, termTf, intrateTf, paymentsTf, amortizeTf;
JButton calcBtn;
JButton clearButton;
JButton exitButton;
JFrame frame;


 public MortgageCalculatorCR62() {
  System.out.println("Mortgage Calculator Change Request #6.");



// public static void MortgageCalculatorCR62(String args[]) {
   
	 String Labels[] = { "7 Years at 5.35%", "15 years at 5.5%", "30 years at 5.75%",};
  

amountLbl = new JLabel("  Loan Amount In Whole Dollars");
termLbl = new JLabel("  Terms In Years");
IntrateLbl = new JLabel("  Interest Rate - Decimal Format (0.000)");
paymentsLbl = new JLabel("  Monthly Payment Amount");

amountTf = new JTextField();
JComboBox comboBox1 = new JComboBox(Labels);

termTf = new JTextField();
intrateTf = new JTextField();

amortizeTf = new JTextField();
amortizeLb1 = new JLabel("Amortization");
paymentsTf = new JTextField();
calcBtn = new JButton("Calculate");
clearButton = new JButton("Clear");
exitButton = new JButton("Exit");




frame = new JFrame("Mortgage Calculator CR#6");
frame.setLayout(new GridLayout(16,16));
frame.add(amountLbl);
frame.add(amountTf);

//Added JcomboBox
frame.add(comboBox1);

/* Remove these frames when action listener is working */
  frame.add(termLbl);
  frame.add(termTf);
  frame.add(IntrateLbl);
  frame.add(intrateTf);
  
  
  
frame.add(paymentsLbl);
frame.add(paymentsTf);


//  Added amortize textfield and label
frame.add(amortizeLb1);
frame.add(amortizeTf);

frame.add(calcBtn);
frame.add(clearButton);
frame.add(exitButton);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clearButton.addActionListener(new Handler());
calcBtn.addActionListener(new Handler());
exitButton.addActionListener(new Handler());
frame.setLocation(300, 300);
frame.pack();
frame.setVisible(true);
}
private class Handler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{


Object command = event.getSource();
if(command == calcBtn)
		{
		  calculate();
		  }
if(command == clearButton)
		{

		  amountTf.setText(null);
		  termTf.setText(null);
		  intrateTf.setText(null);
		  paymentsTf.setText(null);
			 amortizeTf.setText(null);
			 
			}

			if(command == exitButton)
			{
		  System.exit(0);
			 		 
	   
}
}
}

  public static void main(String[] args) {
  new MortgageCalculatorCR62();
}

private void calculate() {
DecimalFormat decimalPlaces=new DecimalFormat("0,000.00");

double amount = Double.parseDouble(amountTf.getText());

/*  Go to if statements below
double term = Double.parseDouble(termTf.getText());
double interest = Double.parseDouble(intrateTf.getText());
double interestRateMonths = interest/12;


/* Next two lines commented out to accomadate formula in else if 
  double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
	paymentsTf.setText("" + decimalPlaces.format(payments)); */
	 
double amortize = amount - payments;
amortizeTf.setText("" + decimalPlaces.format(amortize));



/*  This part of the code is for selecting the loan amount and plugging them into the formula. */

 
//exit application when user presses the exit button
//		Object source = e.getSource();
//		if (source == exitButton)
//		{
//			System.exit(0);
//		}
//calculate payment amount when user presses submit

//  else if (source == calcBtn)
//		{
	//		String Lables = cOption.getSelectedItem();

 String Labels = (String) cOption.getSelectedItem(); // gotta typecast here!!!
	
  if (Labels == "7 years at 5.35%") // this is wrong, but I'll get to that


// else if (Lables == "7 years at 5.35%")
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 7;
				int	months = (years * 12);
				double	interest	= .0535;		
			
			double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

				paymentsTf.setText("" + decimalPlaces.format(payments));

			}			
	 if (Lables == "15 years at 5.5%")
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 15;
				int	months = (years * 12);
				double	interest	= .055;		
		double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

			paymentsTf.setText("" + decimalPlaces.format(payments));
			}
	if (Lables == "30 years at 5.75%")
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 30;
				int	months = (years * 12);
				double	interest	= .0575;		
		double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
		 paymentsTf.setText("" + decimalPlaces.format(payments));
			
		}
		
/* This is the area for the amortization process */



 // displays monthly mortgage payment resulting from above calculation
		System.out.println("\n\nBased on the above criteria, "  +
		 "your monthly payment will be: " +
		  "$" + decimalPlaces.format(payments));

	// formula(s) to calculate monthly interest and principal payments
		monthlyInterestPayment = (amount * interestRateMonths);
		monthlyPrincipalPayment = (payments - monthlyInterestPayment);

   
	// start while loop  
  while (termMonths > 0);
	 
	 
  //  {
  
  
	// information to display
	System.out.println(termMonths + "\t\t$" + decimalPlaces.format(monthlyPrincipalPayment) +
		"\t\t$" + decimalPlaces.format(monthlyInterestPayment) +
		"\t\t$" + decimalPlaces.format(amount)); 

	// decrement months
		termMonths--;

	// calculate interest and principal payments
	monthlyInterestPayment = (amount * interestRateMonths);
	monthlyPrincipalPayment = (payments - monthlyInterestPayment);
	balance = (payments - monthlyPrincipalPayment);
	 
	 System.out.println(balance);

/**/
	 
	 /* Put amortization into a Jtextarea */
	 
	 //payments - principal show each payment

}
}


Was This Post Helpful? 0
  • +
  • -

#7 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: else if statement

Posted 20 March 2009 - 07:23 AM

Help? I'm having a problem with the typecasting and using the else if statement.

/* Mortgage Calculator Change Request #6 in Service Request SR-mf-003. Insert comments in the program to
document the program. Attach a design flow chart to the source code of the program.

Write the program in Java (with a graphical user interface) so that it will allow the user to select which
way they want to calculate a mortgage: by input of the amount of the mortgage, the term of the mortgage,
and the interest rate of the mortgage payment or by input of the amount of a mortgage and then select from
a menu of mortgage loans: 7 year at 5.35%, 15 year at 5.5 %, and 30 year at 5.75%. In either case, display
the mortgage payment amount and then, list the loan balance and interest paid for each payment over the term
of the loan. Allow the user to loop back and enter a new amount and make a new selection, or quit. Insert
comments in the program to document the program.

Author Phil Grabowski

University of Phoenix

PRG421 Java Programming II

Course Date 2/24/09  End Date 3/30/09

Instructor Yining Li  */
 

import java.awt.*;
import java.awt.event.*;

import java.util.*;

import javax.swing.*;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import java.text.NumberFormat;
import java.math.*;

import java.awt.BorderLayout;
import java.awt.Color;

import java.awt.Container;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JCheckBox;
import javax.swing.JFrame;

import javax.swing.JPanel;
import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.JComboBox;
import javax.swing.JFrame;





public class MortgageCalculatorCR62 {

double amount;
double term;
double payments;
double amortize;
double interestRateMonths;
double monthlyInterestPayment;
double monthlyPrincipalPayment;
int termMonths;
double balance;
int Labels;



JLabel amountLbl, termLbl, IntrateLbl, paymentsLbl, amortizeLb1;
JTextField amountTf, termTf, intrateTf, paymentsTf, amortizeTf;
JButton calcBtn;
JButton clearButton;
JButton exitButton;
JFrame frame;


 public MortgageCalculatorCR62() {
  System.out.println("Mortgage Calculator Change Request #6.");



// public static void MortgageCalculatorCR62(String args[]) {
   
	 String Labels[] = { "7 Years at 5.35%", "15 years at 5.5%", "30 years at 5.75%",};
  

amountLbl = new JLabel("  Loan Amount In Whole Dollars");
termLbl = new JLabel("  Terms In Years");
IntrateLbl = new JLabel("  Interest Rate - Decimal Format (0.000)");
paymentsLbl = new JLabel("  Monthly Payment Amount");

amountTf = new JTextField();
JComboBox comboBox1 = new JComboBox(Labels);

Choice cOption = new Choice();


termTf = new JTextField();
intrateTf = new JTextField();

amortizeTf = new JTextField();
amortizeLb1 = new JLabel("Amortization");
paymentsTf = new JTextField();
calcBtn = new JButton("Calculate");
clearButton = new JButton("Clear");
exitButton = new JButton("Exit");
// Labels cOption = new Labels();


frame = new JFrame("Mortgage Calculator CR#6");
frame.setLayout(new GridLayout(16,16));
frame.add(amountLbl);
frame.add(amountTf);

//Added JcomboBox
frame.add(comboBox1);

/* Remove these frames when action listener is working 
  frame.add(termLbl);
  frame.add(termTf);
  frame.add(IntrateLbl);
  frame.add(intrateTf);
  
  */
  
frame.add(paymentsLbl);
frame.add(paymentsTf);


//  Added amortize textfield and label
frame.add(amortizeLb1);
frame.add(amortizeTf);

frame.add(calcBtn);
frame.add(clearButton);
frame.add(exitButton);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clearButton.addActionListener(new Handler());
calcBtn.addActionListener(new Handler());
exitButton.addActionListener(new Handler());
frame.setLocation(300, 300);
frame.pack();
frame.setVisible(true);
}
private class Handler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{


Object command = event.getSource();
if(command == calcBtn)
		{
		  calculate();
		  }
if(command == clearButton)
		{

		  amountTf.setText(null);
		  termTf.setText(null);
		  intrateTf.setText(null);
		  paymentsTf.setText(null);
			 amortizeTf.setText(null);
			 
			}

			if(command == exitButton)
			{
		  System.exit(0);
			 		 
	   
}
}
}

  public static void main(String[] args) {
  new MortgageCalculatorCR62();
}

private void calculate() {
DecimalFormat decimalPlaces=new DecimalFormat("0,000.00");

for(int i = 0; i < Lables.length; i++)
					cOption.add(Labels[i]);

// double amount = Double.parseDouble(amountTf.getText());

/*  Go to if statements below
double term = Double.parseDouble(termTf.getText());
double interest = Double.parseDouble(intrateTf.getText());
double interestRateMonths = interest/12;


/* Next two lines commented out to accomadate formula in else if 
  double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
	paymentsTf.setText("" + decimalPlaces.format(payments)); */
	 
//  double amortize = amount - payments;
//  amortizeTf.setText("" + decimalPlaces.format(amortize)); 



/*  This part of the code is for selecting the loan amount and plugging them into the formula. */

 
//exit application when user presses the exit button
//		Object source = e.getSource();
//		if (source == exitButton)
//		{
//			System.exit(0);
//		}
//calculate payment amount when user presses submit

//  else if (source == calcBtn)
//		{

{
			String Lables = cOption.getSelectedItem();
	
		

// String Labels = (String) cOption.getSelectedItem(); // gotta typecast here!!!
	
   if (Labels == "7 years at 5.35%") // this is wrong, but I'll get to that


// else if (Lables == "7 years at 5.35%")
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 7;
				int	months = (years * 12);
				double	interest	= .0535;		
			
			double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

				paymentsTf.setText("" + decimalPlaces.format(payments));

			}			
	 if (Lables == "15 years at 5.5%")
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 15;
				int	months = (years * 12);
				double	interest	= .055;		
		double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

			paymentsTf.setText("" + decimalPlaces.format(payments));
			}
	if (Lables == "30 years at 5.75%")
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 30;
				int	months = (years * 12);
				double	interest	= .0575;		
		double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
		 paymentsTf.setText("" + decimalPlaces.format(payments));
			
		}
		
/* This is the area for the amortization process */



 // displays monthly mortgage payment resulting from above calculation
		System.out.println("\n\nBased on the above criteria, "  +
		 "your monthly payment will be: " +
		  "$" + decimalPlaces.format(payments));

	// formula(s) to calculate monthly interest and principal payments
		monthlyInterestPayment = (amount * interestRateMonths);
		monthlyPrincipalPayment = (payments - monthlyInterestPayment);

   
	// start while loop  
  while (termMonths > 0);
	 
	 
  //  {
  
  
	// information to display
	System.out.println(termMonths + "\t\t$" + decimalPlaces.format(monthlyPrincipalPayment) +
		"\t\t$" + decimalPlaces.format(monthlyInterestPayment) +
		"\t\t$" + decimalPlaces.format(amount)); 

	// decrement months
		termMonths--;

	// calculate interest and principal payments
	monthlyInterestPayment = (amount * interestRateMonths);
	monthlyPrincipalPayment = (payments - monthlyInterestPayment);
	balance = (payments - monthlyPrincipalPayment);
	 
	 System.out.println(balance);

/**/
	 
	 /* Put amortization into a Jtextarea */
	 
	 //payments - principal show each payment

}
}
}



Was This Post Helpful? 0
  • +
  • -

#8 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: else if statement

Posted 20 March 2009 - 10:22 AM

Ok, first off...where is the source code for the class Choice? You made the variable cOption that type...but I don't see the code anywhere. You might have wanted to make it a JComboBox. And it needs to be an instance variable.

JLabel amountLbl, termLbl, IntrateLbl, paymentsLbl, amortizeLb1;
JTextField amountTf, termTf, intrateTf, paymentsTf, amortizeTf;
JButton calcBtn;
JButton clearButton;
JButton exitButton;
JFrame frame;

// these are all your swing package instance variables...add this one

JComboBox cOption;



And later in the constructor...

public MortgageCalculatorCR62() {

// get rid of this one.
JComboBox comboBox1 = new JComboBox(Labels);

Choice cOption = new Choice(); // change this to the next line

cOption = new JComboBox(labels);
}



Now then...comparing if (Lables == "7 years at 5.35%") is NOT right. You HAVE to use the equals() method.

if (Lables.equals("7 years at 5.35%"))
{
    // ...
}

else if (Lables.equals("15 years at 5.5%"))
{
    // ...
}

else if (Lables.equals("30 years at 5.75%"))
{
    // ...
}


That should also clear up the else if problems as well.

Hope this helps! :)
Was This Post Helpful? 0
  • +
  • -

#9 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: else if statement

Posted 20 March 2009 - 11:23 AM

Well I believe I followed your post to the word, but something still isn't right.


/* Mortgage Calculator Change Request #6 in Service Request SR-mf-003. Insert comments in the program to
document the program. Attach a design flow chart to the source code of the program.

Write the program in Java (with a graphical user interface) so that it will allow the user to select which
way they want to calculate a mortgage: by input of the amount of the mortgage, the term of the mortgage,
and the interest rate of the mortgage payment or by input of the amount of a mortgage and then select from
a menu of mortgage loans: 7 year at 5.35%, 15 year at 5.5 %, and 30 year at 5.75%. In either case, display
the mortgage payment amount and then, list the loan balance and interest paid for each payment over the term
of the loan. Allow the user to loop back and enter a new amount and make a new selection, or quit. Insert
comments in the program to document the program.

Author Phil Grabowski

University of Phoenix

PRG421 Java Programming II

Course Date 2/24/09  End Date 3/30/09

Instructor Yining Li  */
 

import java.awt.*;
import java.awt.event.*;

import java.util.*;

import javax.swing.*;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import java.text.NumberFormat;
import java.math.*;

import java.awt.BorderLayout;
import java.awt.Color;

import java.awt.Container;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JCheckBox;
import javax.swing.JFrame;

import javax.swing.JPanel;
import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.JComboBox;
import javax.swing.JFrame;





public class MortgageCalculatorCR62 {

double amount;
double term;
double payments;
double amortize;
double interestRateMonths;
double monthlyInterestPayment;
double monthlyPrincipalPayment;
int termMonths;
double balance;
int Labels;


// Instance Variables
JLabel amountLbl, termLbl, IntrateLbl, paymentsLbl, amortizeLb1;
JTextField amountTf, termTf, intrateTf, paymentsTf, amortizeTf;
JButton calcBtn;
JButton clearButton;
JButton exitButton;
JFrame frame;
JComboBox cOption;



 public MortgageCalculatorCR62() {
  System.out.println("Mortgage Calculator Change Request #6.");



// public static void MortgageCalculatorCR62(String args[]) {
   
	 String Labels[] = { "7 Years at 5.35%", "15 years at 5.5%", "30 years at 5.75%",};
  

amountLbl = new JLabel("  Loan Amount In Whole Dollars");
termLbl = new JLabel("  Terms In Years");
IntrateLbl = new JLabel("  Interest Rate - Decimal Format (0.000)");
paymentsLbl = new JLabel("  Monthly Payment Amount");

amountTf = new JTextField();
//  JComboBox comboBox1 = new JComboBox(Labels);

//  Choice cOption = new Choice();

cOption = new JComboBox(Labels);




termTf = new JTextField();
intrateTf = new JTextField();

amortizeTf = new JTextField();
amortizeLb1 = new JLabel("Amortization");
paymentsTf = new JTextField();
calcBtn = new JButton("Calculate");
clearButton = new JButton("Clear");
exitButton = new JButton("Exit");
// Labels cOption = new Labels();


frame = new JFrame("Mortgage Calculator CR#6");
frame.setLayout(new GridLayout(16,16));
frame.add(amountLbl);
frame.add(amountTf);

//Added JcomboBox
//  frame.add(comboBox1);

//  frame.add(jComboBox);

/* Remove these frames when action listener is working 
  frame.add(termLbl);
  frame.add(termTf);
  frame.add(IntrateLbl);
  frame.add(intrateTf);
  
  */
  
frame.add(paymentsLbl);
frame.add(paymentsTf);


//  Added amortize textfield and label
frame.add(amortizeLb1);
frame.add(amortizeTf);

frame.add(calcBtn);
frame.add(clearButton);
frame.add(exitButton);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clearButton.addActionListener(new Handler());
calcBtn.addActionListener(new Handler());
exitButton.addActionListener(new Handler());
frame.setLocation(300, 300);
frame.pack();
frame.setVisible(true);
}
private class Handler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{


Object command = event.getSource();
if(command == calcBtn)
		{
		  calculate();
		  }
if(command == clearButton)
		{

		  amountTf.setText(null);
		  termTf.setText(null);
		  intrateTf.setText(null);
		  paymentsTf.setText(null);
			 amortizeTf.setText(null);
			 
			}

			if(command == exitButton)
			{
		  System.exit(0);
			 		 
	   
}
}
}

  public static void main(String[] args) {
  new MortgageCalculatorCR62();
}

private void calculate() {
DecimalFormat decimalPlaces=new DecimalFormat("0,000.00");

   for(int i = 0; i < Labels.length; i++);
					cOption.add(Labels[i]);

// double amount = Double.parseDouble(amountTf.getText());

/*  Go to if statements below
double term = Double.parseDouble(termTf.getText());
double interest = Double.parseDouble(intrateTf.getText());
double interestRateMonths = interest/12;


/* Next two lines commented out to accomadate formula in else if 
  double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
	paymentsTf.setText("" + decimalPlaces.format(payments)); */
	 
//  double amortize = amount - payments;
//  amortizeTf.setText("" + decimalPlaces.format(amortize)); 



/*  This part of the code is for selecting the loan amount and plugging them into the formula. */

 
//exit application when user presses the exit button
//		Object source = e.getSource();
//		if (source == exitButton)
//		{
//			System.exit(0);
//		}
//calculate payment amount when user presses submit

//  else if (source == calcBtn)
//		{

{
			String Lables = cOption.getSelectedItem();
	
		

// String Labels = (String) cOption.getSelectedItem(); // gotta typecast here!!!
	
   if (Labels.equals("7 years at 5.35%")) // this is wrong, but I'll get to that


// else if (Lables == "7 years at 5.35%")
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 7;
				int	months = (years * 12);
				double	interest	= .0535;		
			
			double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

				paymentsTf.setText("" + decimalPlaces.format(payments));

			}			
	 if (Lables.equals("15 years at 5.5%"))
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 15;
				int	months = (years * 12);
				double	interest	= .055;		
		double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

			paymentsTf.setText("" + decimalPlaces.format(payments));
			}
	if (Lables.equals("30 years at 5.75%"))
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 30;
				int	months = (years * 12);
				double	interest	= .0575;		
		double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
		 paymentsTf.setText("" + decimalPlaces.format(payments));
			
		}
		
/* This is the area for the amortization process */



 // displays monthly mortgage payment resulting from above calculation
		System.out.println("\n\nBased on the above criteria, "  +
		 "your monthly payment will be: " +
		  "$" + decimalPlaces.format(payments));

	// formula(s) to calculate monthly interest and principal payments
		monthlyInterestPayment = (amount * interestRateMonths);
		monthlyPrincipalPayment = (payments - monthlyInterestPayment);

   
	// start while loop  
  while (termMonths > 0);
	 
	 
  //  {
  
  
	// information to display
	System.out.println(termMonths + "\t\t$" + decimalPlaces.format(monthlyPrincipalPayment) +
		"\t\t$" + decimalPlaces.format(monthlyInterestPayment) +
		"\t\t$" + decimalPlaces.format(amount)); 

	// decrement months
		termMonths--;

	// calculate interest and principal payments
	monthlyInterestPayment = (amount * interestRateMonths);
	monthlyPrincipalPayment = (payments - monthlyInterestPayment);
	balance = (payments - monthlyPrincipalPayment);
	 
	 System.out.println(balance);

/**/
	 
	 /* Put amortization into a Jtextarea */
	 
	 //payments - principal show each payment


}}}


Was This Post Helpful? 0
  • +
  • -

#10 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: else if statement

Posted 20 March 2009 - 11:26 AM

View Postgrabowpd, on 20 Mar, 2009 - 12:23 PM, said:

Well I believe I followed your post to the word, but something still isn't right.


What isn't right?

Ok, I copied/compiled your code...

And the first error int cannot be dereferenced pointing at the for loop right at the beginning of the calculate() method. You need to move that for loop into the constructor somewhere. You can move it anywhere into the constructor after you instantiate cOption. You also have a semicolon right after the for line. That needs to be removed.

Next, there is also a problem with the method add() that you call in that for loop. The method should be addItem(Labels[i]);.

Now then...in the calculate() method.
String Lables = cOption.getSelectedItem(); needs to be --> String Lables = (String) cOption.getSelectedItem();

Now, another int cannot be dereferenced error in the if statement just below the getSelectedItem() call. Don't confuse Labels with Lables. Switch them.

That takes care of all the errors! :D

This post has been edited by Locke: 20 March 2009 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

#11 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: else if statement

Posted 20 March 2009 - 11:57 AM

Locke,

I really appreciate you taking the time to help me. I'm really losing my mind. I'm trying to understand what you were telling me in the last post. My head is swimming. I would rather write ten papers than write code. This just isn't any fun for me. It's just frustrating.

Not to mention how the soon to be x is acting today.

I'm doing my best to understand. I saw where I kept messing up with Labels.
Was This Post Helpful? 0
  • +
  • -

#12 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: else if statement

Posted 20 March 2009 - 12:00 PM

View Postgrabowpd, on 20 Mar, 2009 - 12:57 PM, said:

Locke,

I really appreciate you taking the time to help me. I'm really losing my mind. I'm trying to understand what you were telling me in the last post. My head is swimming. I would rather write ten papers than write code. This just isn't any fun for me. It's just frustrating.

Not to mention how the soon to be x is acting today.

I'm doing my best to understand. I saw where I kept messing up with Labels.


Don't worry about it. :) Just look at the compiler errors/warnings very carefully and what they're telling you. If you hit one where you can't figure out what it means or how to fix it, I (and others, I would assume) would be glad to help.

I'm a nerd on spring break...I've got nothing better to do. ;)

As for writing ten papers opposed to code...I would probably have to kill myself first. :crazy: I'd take the code any day. So rest assured, you have someone helping you that likes to write code. :D

This post has been edited by Locke: 20 March 2009 - 12:03 PM

Was This Post Helpful? 0
  • +
  • -

#13 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: else if statement

Posted 20 March 2009 - 12:28 PM

Still got three errors.


/* Mortgage Calculator Change Request #6 in Service Request SR-mf-003. Insert comments in the program to
document the program. Attach a design flow chart to the source code of the program.

Write the program in Java (with a graphical user interface) so that it will allow the user to select which
way they want to calculate a mortgage: by input of the amount of the mortgage, the term of the mortgage,
and the interest rate of the mortgage payment or by input of the amount of a mortgage and then select from
a menu of mortgage loans: 7 year at 5.35%, 15 year at 5.5 %, and 30 year at 5.75%. In either case, display
the mortgage payment amount and then, list the loan balance and interest paid for each payment over the term
of the loan. Allow the user to loop back and enter a new amount and make a new selection, or quit. Insert
comments in the program to document the program.

Author Phil Grabowski

University of Phoenix

PRG421 Java Programming II

Course Date 2/24/09  End Date 3/30/09

Instructor Yining Li  */


import java.awt.*;
import java.awt.event.*;

import java.util.*;

import javax.swing.*;
import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import java.text.NumberFormat;
import java.math.*;

import java.awt.BorderLayout;
import java.awt.Color;

import java.awt.Container;
import java.awt.FlowLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JCheckBox;
import javax.swing.JFrame;

import javax.swing.JPanel;
import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.JComboBox;
import javax.swing.JFrame;





public class MortgageCalculatorCR62 {

double amount;
double term;
double payments;
double amortize;
double interestRateMonths;
double monthlyInterestPayment;
double monthlyPrincipalPayment;
int termMonths;
double balance;
int Labels;


// Instance Variables
JLabel amountLbl, termLbl, IntrateLbl, paymentsLbl, amortizeLb1;
JTextField amountTf, termTf, intrateTf, paymentsTf, amortizeTf;
JButton calcBtn;
JButton clearButton;
JButton exitButton;
JFrame frame;
JComboBox cOption;



public MortgageCalculatorCR62() {
  System.out.println("Mortgage Calculator Change Request #6.");



// public static void MortgageCalculatorCR62(String args[]) {
   
	 String Labels[] = { "7 Years at 5.35%", "15 years at 5.5%", "30 years at 5.75%",};
  

amountLbl = new JLabel("  Loan Amount In Whole Dollars");
termLbl = new JLabel("  Terms In Years");
IntrateLbl = new JLabel("  Interest Rate - Decimal Format (0.000)");
paymentsLbl = new JLabel("  Monthly Payment Amount");

amountTf = new JTextField();
//  JComboBox comboBox1 = new JComboBox(Labels);

//  Choice cOption = new Choice();

cOption = new JComboBox(Labels);




termTf = new JTextField();
intrateTf = new JTextField();

amortizeTf = new JTextField();
amortizeLb1 = new JLabel("Amortization");
paymentsTf = new JTextField();
calcBtn = new JButton("Calculate");
clearButton = new JButton("Clear");
exitButton = new JButton("Exit");
// Labels cOption = new Labels();


frame = new JFrame("Mortgage Calculator CR#6");
frame.setLayout(new GridLayout(16,16));
frame.add(amountLbl);
frame.add(amountTf);

//Added JcomboBox
//  frame.add(comboBox1);

//  frame.add(jComboBox);

/* Remove these frames when action listener is working 
  frame.add(termLbl);
  frame.add(termTf);
  frame.add(IntrateLbl);
  frame.add(intrateTf);
  
  */
  
frame.add(paymentsLbl);
frame.add(paymentsTf);


//  Added amortize textfield and label
frame.add(amortizeLb1);
frame.add(amortizeTf);

frame.add(calcBtn);
frame.add(clearButton);
frame.add(exitButton);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clearButton.addActionListener(new Handler());
calcBtn.addActionListener(new Handler());
exitButton.addActionListener(new Handler());
frame.setLocation(300, 300);
frame.pack();
frame.setVisible(true);
}
private class Handler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{


Object command = event.getSource();
if(command == calcBtn)
		{
		  calculate();
		  }
if(command == clearButton)
		{

		  amountTf.setText(null);
		  termTf.setText(null);
		  intrateTf.setText(null);
		  paymentsTf.setText(null);
			 amortizeTf.setText(null);
			 
			}

			if(command == exitButton)
			{
		  System.exit(0);
					  
	   
}
}
}

  public static void main(String[] args) {
  new MortgageCalculatorCR62();
}

private void calculate() {
DecimalFormat decimalPlaces=new DecimalFormat("0,000.00");


/* Moved for loop into constructor after intiating cOption */
 //  for(int i = 0; i < Labels.length; i++)
	   //			 cOption.add(Labels[i]);

// double amount = Double.parseDouble(amountTf.getText());

/*  Go to if statements below
double term = Double.parseDouble(termTf.getText());
double interest = Double.parseDouble(intrateTf.getText());
double interestRateMonths = interest/12;


/* Next two lines commented out to accomadate formula in else if 
  double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
	paymentsTf.setText("" + decimalPlaces.format(payments)); */
	 
//  double amortize = amount - payments;
//  amortizeTf.setText("" + decimalPlaces.format(amortize)); 



/*  This part of the code is for selecting the loan amount and plugging them into the formula. */


//exit application when user presses the exit button
//		Object source = e.getSource();
//		if (source == exitButton)
//		{
//			System.exit(0);
//		}
//calculate payment amount when user presses submit

//  else if (source == calcBtn)
//		{

{
   // String Lables = cOption.getSelectedItem();// this is wrong replace with String Labels = (Sting) cOption.getSelectedItem
	//
	
	String Labels = (String) cOption.getSelectedItem(); // gotta typecast here!!!
	
	   for(int i = 0; i < Labels.length; i++) 
		 
		 addItem(Labels[i]);
		 
//		  cOption.add(Labels[i]);

   
   if (Labels.equals("7 years at 5.35%")) 
	

// else if (Lables == "7 years at 5.35%") // this is wrong, but I'll get to that
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 7;
				int	months = (years * 12);
				double	interest	= .0535;		
			
			double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

				paymentsTf.setText("" + decimalPlaces.format(payments));

			}			
	 if (Labels.equals("15 years at 5.5%"))
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 15;
				int	months = (years * 12);
				double	interest	= .055;		
		double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));

			paymentsTf.setText("" + decimalPlaces.format(payments));
			}
	if (Lables.equals("30 years at 5.75%"))
			{			
				double amount = Double.parseDouble(amountTf.getText());			
				int	years = 30;
				int	months = (years * 12);
				double	interest	= .0575;		
		double payments = (amount * ((interest/12) / (1-Math.pow((1+(interest/12)),-(term*12)))));
		 paymentsTf.setText("" + decimalPlaces.format(payments));
			
		}
		
/* This is the area for the amortization process */



// displays monthly mortgage payment resulting from above calculation
		System.out.println("\n\nBased on the above criteria, "  +
		 "your monthly payment will be: " +
		  "$" + decimalPlaces.format(payments));

	// formula(s) to calculate monthly interest and principal payments
		monthlyInterestPayment = (amount * interestRateMonths);
		monthlyPrincipalPayment = (payments - monthlyInterestPayment);

   
	// start while loop  
  while (termMonths > 0);
	 
	 
  //  {
  
  
	// information to display
	System.out.println(termMonths + "\t\t$" + decimalPlaces.format(monthlyPrincipalPayment) +
		"\t\t$" + decimalPlaces.format(monthlyInterestPayment) +
		"\t\t$" + decimalPlaces.format(amount)); 

	// decrement months
		termMonths--;

	// calculate interest and principal payments
	monthlyInterestPayment = (amount * interestRateMonths);
	monthlyPrincipalPayment = (payments - monthlyInterestPayment);
	balance = (payments - monthlyPrincipalPayment);
	 
	 System.out.println(balance);

/**/
	 
	 /* Put amortization into a Jtextarea */
	 
	 //payments - principal show each payment


}}}



Was This Post Helpful? 0
  • +
  • -

#14 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: else if statement

Posted 20 March 2009 - 12:37 PM

View Postgrabowpd, on 20 Mar, 2009 - 01:28 PM, said:

Still got three errors.


Details, man, details! :D
Was This Post Helpful? 0
  • +
  • -

#15 grabowpd  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 06-March 09

Re: else if statement

Posted 21 March 2009 - 10:06 AM

View PostLocke, on 20 Mar, 2009 - 11:37 AM, said:

View Postgrabowpd, on 20 Mar, 2009 - 01:28 PM, said:

Still got three errors.


Details, man, details! :D


I quess the code is having problems converting the string to an integer.

Here are the compile errors.

MortgageCalculatorCR62.java:244: cannot find symbol
symbol : variable length
location: class java.lang.String
for(int i = 0; i < Labels.length; i++)
^
MortgageCalculatorCR62.java:246: array required, but java.lang.String found
addItem(Labels[i]);
^
MortgageCalculatorCR62.java:276: cannot find symbol
symbol : variable Lables
location: class MortgageCalculatorCR62
if (Lables.equals("30 years at 5.75%"))
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2