I have my brackets all messed up and was wondering if someone could help me fix them. I am sure there are other errors too, so any advice would be greatly appreciated! :)

import java.util.*;
import java.awt.Container;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.ComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class DeeWeek3_J2
{
	private static int total;
	private static int interestPay;
	public static void main(String[]args, int counter, NumberFormat moneyFormat)throws IOException
	{
	System.out.println("Mortgage Calculator");
	
	}	
	
	//variables
	double loanAmt = 200000.00;
	int[]length = new int [3];
	{
		length[0]=7;
		length[1]=15;
		length[2]=30;
	}
	
		private JComboBox IntInterest;
		private JLabel principallb, interestlb, termlb, paymentlb;
		JButton calBtn;
		private JButton resetBtn;
		private JTextField principalTxFld, interestTxFld, termTxFld, paymentTxFld;
		private TextArea payAmortTA;
		
		String currentInt;
		private JTextField payment;
		
		{
	double [] interest = new double [3]{
		                          
		interest[0]=.0535,interest[1]=.0550,interest[2]=.0575};
		{
	int x=0;
	
	DecimalFormat twoplaces=new DecimalFormat("#,###.00");
	
	//Combo Items
	String[] comboItems={"5.35","5.50","5.75"};
	
			
	//Labels
	principallb = new JLabel("Principal:");
	interestlb = new JLabel("Interest:");
	termlb = new JLabel("Term:");
	paymentlb = new JLabel("Payment:");
	
	//Text Field
	principalTxFld = new JTextField("",10);
	interestTxFld = new JTextField("",5);
	interestTxFld.setEditable(true);
	termTxFld = new JTextField("",5);
	payment = new JTextField("",10);
	payment.setEditable(true);
	
	
	{
		// Pull down table
	IntInterest = new JComboBox(comboItems);
	IntInterest.setEditable(true);
	payAmortTA = new TextArea(20,40);
	}
	
			calculateMortgage();
		}
		{
		 createAndShowGui();
			
			JFrame frame = new JFrame("Morgage Calculator");
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
			Object calculator = new MortgageCalculator();
			((JComponent) calculator).setOpaque(true);
			frame.setContentPane((Container) calculator);
			
			frame.pack();
			frame.setVisible(true);}
		}
		private void calculateMortgage(){
		}
		private void createAndShowGui() {
			// TODO Auto-generated method stub
			
		}
		{
						
		};
	{
		
		//Buttons
		calBtn = new JButton("Calculate");
		calBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae)
			{
	{
	resetBtn = new JButton("Reset");
		resetBtn.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				{
					principalTxFld.setText("");
					interestTxFld.setText("");
					termTxFld.setText("");
					monthlyPayment.setText("");
					payAmortTA.setText.setText("");
}
		}
		
{
	//Adding the Labels and Fields
	add(principallb);
	add(principalTxFld);
	add(interestlb);
	add(interestTxFld);
	add(termlb);
	add(termTxFld);
	add(paymentlb);
	add(paymentTxFld);
	add(payAmortTA);
	add(calBtn);
	add(resetBtn);
	{
	
	//Calculation for Payments
	do
	{
		double payment=(loanAmt*(interest[x]/12))/(1-(Math.pow(1/(1+(interest[x]/12)),(length[x]*12))));
			System.out.println();
			System.out.print("Loan Principal Amount: $200,000.00");
			System.out.print("Payment Amount:$"+twoplaces.format(payment)+"for the "+length[x]+"-Year Contract.");
			
			x++;
	}
	while(x<3);
	{
	
	int amount;
	for (int i=0; i<=11;i++)
		interestPay=amount*interest;
		amount-=(total-interestPay);
		
		System.out.println();
		System.out.print("Payment#"+counter++);
		System.out.print("\tPrincipal Balance:v"+moneyFormat.format(amount));
		System.out.print("\tMonth Interest:"+moneyFormat.format(interest));
		System.out.println();
		System.out.print("Press Y and hit enter...");
		
		counter=1;
	}
		while(amount>=1)
		{
			//loop statement to show 12 lines at a time
			for(int i=0;1<=11;i++)
			{
				//payment output
				interestPay=amount*interest;
				amount-=(total-interest);
				
				System.out.println();
				System.out.print("Payment#"+counter++);
				System.out.print("\tPrincipal Balance:"+moneyFormat.format(interest));
				System.out.println();
			}
			
			System.out.println();
			System.out.print("Press Y and hit enter...");
			
			}
	}
}
		}
	}
		}
		}

Recommended Answers

All 6 Replies

If you are not using NetBeans or something similar, start using it. It has a nce code formatting feature. here's your code, formatted in one click, with NetBeans:

import java.util.*;
import java.awt.Container;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.ComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class DeeWeek3_J2
{

    private static int total;
    private static int interestPay;

    public static void main(String[] args, int counter, NumberFormat moneyFormat) throws IOException
    {
        System.out.println("Mortgage Calculator");

    }
    //variables
    double loanAmt = 200000.00;
    int[] length = new int[3];


    {
        length[0] = 7;
        length[1] = 15;
        length[2] = 30;
    }
    private JComboBox IntInterest;
    private JLabel principallb,  interestlb,  termlb,  paymentlb;
    JButton calBtn;
    private JButton resetBtn;
    private JTextField principalTxFld,  interestTxFld,  termTxFld,  paymentTxFld;
    private TextArea payAmortTA;
    String currentInt;
    private JTextField payment;


    {
        double[] interest = new double[3]
        {

            interest[0] = .0535
            ,interest[1] = .0550
            ,interest[2] = .0575
        }
        ;
        {
            int x = 0;

            DecimalFormat twoplaces = new DecimalFormat("#,###.00");

            //Combo Items
            String[] comboItems =
            {
                "5.35", "5.50", "5.75"
            };


            //Labels
            principallb = new JLabel("Principal:");
            interestlb = new JLabel("Interest:");
            termlb = new JLabel("Term:");
            paymentlb = new JLabel("Payment:");

            //Text Field
            principalTxFld = new JTextField("", 10);
            interestTxFld = new JTextField("", 5);
            interestTxFld.setEditable(true);
            termTxFld = new JTextField("", 5);
            payment = new JTextField("", 10);
            payment.setEditable(true);
            {
                // Pull down table
                IntInterest = new JComboBox(comboItems);
                IntInterest.setEditable(true);
                payAmortTA = new TextArea(20, 40);
            }

            calculateMortgage();
        }
        {
            createAndShowGui();

            JFrame frame = new JFrame("Morgage Calculator");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Object calculator = new MortgageCalculator();
            ((JComponent) calculator).setOpaque(true);
            frame.setContentPane((Container) calculator);

            frame.pack();
            frame.setVisible(true);
        }
    }

    private void calculateMortgage()
    {
    }

    private void createAndShowGui()
    {
        // TODO Auto-generated method stub
    }


    {
    }
    ;

    {

        //Buttons
        calBtn = new JButton("Calculate");
        calBtn.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent ae)
            {
                {
                    resetBtn = new JButton("Reset");
                    resetBtn.addActionListener(new ActionListener()
                    {

                        public void actionPerformed(ActionEvent ae)
                        {
                            {
                                principalTxFld.setText("");
                                interestTxFld.setText("");
                                termTxFld.setText("");
                                monthlyPayment.setText("");
                                payAmortTA.setText.setText("");
                            }
                        }


                        {
                            //Adding the Labels and Fields
                            add(principallb);
                            add(principalTxFld);
                            add(interestlb);
                            add(interestTxFld);
                            add(termlb);
                            add(termTxFld);
                            add(paymentlb);
                            add(paymentTxFld);
                            add(payAmortTA);
                            add(calBtn);
                            add(resetBtn);
                            {

                                //Calculation for Payments
                                do
                                {
                                    double payment = (loanAmt * (interest[x] / 12)) / (1 - (Math.pow(1 / (1 + (interest[x] / 12)), (length[x] * 12))));
                                    System.out.println();
                                    System.out.print("Loan Principal Amount: $200,000.00");
                                    System.out.print("Payment Amount:$" + twoplaces.format(payment) + "for the " + length[x] + "-Year Contract.");

                                    x++;
                                }
                                while (x < 3);
                                {

                                    int amount;
                                    for (int i = 0; i <= 11; i++)
                                    {
                                        interestPay = amount * interest;
                                    }
                                    amount -= (total - interestPay);

                                    System.out.println();
                                    System.out.print("Payment#" + counter++);
                                    System.out.print("\tPrincipal Balance:v" + moneyFormat.format(amount));
                                    System.out.print("\tMonth Interest:" + moneyFormat.format(interest));
                                    System.out.println();
                                    System.out.print("Press Y and hit enter...");

                                    counter = 1;
                                }
                                while (amount >= 1)
                                {
                                    //loop statement to show 12 lines at a time
                                    for (int i = 0; 1 <= 11; i++)
                                    {
                                        //payment output
                                        interestPay = amount * interest;
                                        amount -= (total - interest);

                                        System.out.println();
                                        System.out.print("Payment#" + counter++);
                                        System.out.print("\tPrincipal Balance:" + moneyFormat.format(interest));
                                        System.out.println();
                                    }

                                    System.out.println();
                                    System.out.print("Press Y and hit enter...");

                                }
                            }
                        }
                    }

                }
            }
        }

You have a bunch of code floating around inside your class which doesn't seem to belong to any function and brackets that appear to serve no purpose. Is there supposed to be a constructor somewhere where a lot of this code is supposed to go?

No that is my entire code!
I am not sure of how to code I am very, very new to java. This is the requirements for my code:

"Write a program in Java with a GUI and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage and the users selection from a menu of available loans:
7 yrs @ 5.35%
15 yrs @ 5.50%
30 yrs @ 5.75%

Use an array for the mortgage data for the different loans. Display the mortgage payment amount followed by 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.

Can you give me generic examples of what I need that I may not have? I am very confused and only 2 days to get this right.

I am so sorry, Thank you very much for taking time to assist me! I really appreciate it!

I cleaned your code up a bit and changed it so it made a little more sense. There's still a lot for you to do. I deleted some code that you can stick back it, but the organization needed a complete overhaul. This is ONE way of doing things, though not the only way.

Main rules:

  1. Have your class extend JFrame.
  2. Have your class implement an ActionListener.
  3. Have main be short. Just calls the constructor.
  4. Have the constructor be short. Basically calls the GUI setup function.
  5. Get rid of excess brackets.
  6. Get rid of static variables till you're more experienced.
  7. No "floating" code. Everything is inside of a function.
  8. Semicolons after every line of code.
import java.util.*;
import java.awt.Container;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.swing.ComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class DeeWeek3_J2 extends JFrame implements ActionListener
{
    private int total;
    private int interestPay;

    //variables
    double loanAmt = 200000.00;
    int[] length = new int[3];
    double[] interest = new double[3];
    //Combo Items
    String[] comboItems =
    {
        "5.35", "5.50", "5.75"
    };
    private JComboBox IntInterest;
    private JLabel principallb,  interestlb,  termlb,  paymentlb;
    JButton calBtn;
    private JButton resetBtn;
    private JTextField principalTxFld,  interestTxFld,  termTxFld,  paymentTxFld;
    private TextArea payAmortTA;
    String currentInt;
    private JTextField payment;

    public static void main(String[] args, int counter, NumberFormat moneyFormat) throws IOException
    {
        DeeWeek3_J2 frame = new DeeWeek3_J2();
    }

    public DeeWeek3_J2()
    {
        createAndShowGui();
        calculateMortgage();
    }

    private void calculateMortgage()
    {
        // put mortgage calculation code here
    }

    private void createAndShowGui()
    {
        setTitle("Mortgage Calculator");

        interest[0] = .0535;
        interest[1] = .0550;
        interest[2] = .0575;
        length[0] = 7;
        length[1] = 15;
        length[2] = 30;

        int x = 0;

        DecimalFormat twoplaces = new DecimalFormat("#,###.00");

        //Labels
        principallb = new JLabel("Principal:");
        interestlb = new JLabel("Interest:");
        termlb = new JLabel("Term:");
        paymentlb = new JLabel("Payment:");

        //Text Field
        principalTxFld = new JTextField("", 10);
        interestTxFld = new JTextField("", 5);
        interestTxFld.setEditable(true);
        termTxFld = new JTextField("", 5);
        payment = new JTextField("", 10);
        payment.setEditable(true);

        // Pull down table
        IntInterest = new JComboBox(comboItems);
        IntInterest.setEditable(true);
        payAmortTA = new TextArea(20, 40);


        //Buttons
        calBtn = new JButton("Calculate");
        calBtn.addActionListener(this);
        resetBtn = new JButton("Reset");
        resetBtn.addActionListener(this);

        //Adding the Labels and Fields
        add(principallb);
        add(principalTxFld);
        add(interestlb);
        add(interestTxFld);
        add(termlb);
        add(termTxFld);
        add(paymentlb);
        add(paymentTxFld);
        add(payAmortTA);
        add(calBtn);
        add(resetBtn);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        // stick all of your actionPerformed stuff in here
    }
}

Ok, so I modified what I needed with what you sent and I am confused about the errors I am getting in the part I added back in.

The errors are:

The operator(/,-,*it really doesnt matter what it is get the same error) is undefined.

Could you please share with me what that means so I can fix it?
I think for every step I take forward I slip back 2!!

Again thank you so much!

import java.awt.TextArea;
import java.awt.TextComponent;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.io.IOException;
      import java.text.DecimalFormat;
      import java.text.NumberFormat;
   
      import javax.swing.JButton;
      import javax.swing.JComboBox;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
import javax.swing.JTextField;
 
      @SuppressWarnings("serial")
	public class main extends JFrame implements ActionListener
       {
	  private static final String MonthTerm = null;
	private static final double Months = 0;
	//variables
      double loanAmt = 200000.00;
      int[] length = new int[3];
      double[] interest = new double[3];

      //Combo Items
      String[] comboItems =
       {
     "5.35", "5.50", "5.75"
     };
  
      private JComboBox IntInterest;
      private JLabel principallb, interestlb, termlb, paymentlb;
      JButton calBtn;
      private JButton resetBtn;
      private JTextField principalTxFld, interestTxFld, termTxFld, paymentTxFld;
      private TextArea payAmortTA;
      String currentInt;
      private JTextField payment;
        
      public main(final String[] args, final int counter, final NumberFormat moneyFormat) throws IOException
      {
      new main();
      }
       
      public main()
      {
      createAndShowGui();
      calculateMortgage();
      }
  
  private void calculateMortgage()
      {
	  
      // put mortgage calculation code here
      }
      private void createAndShowGui()
      {
	  
      setTitle("Mortgage Calculator");
      interest[0] = .0535;
      interest[1] = .0550;
      interest[2] = .0575;
      length[0] = 7;
      length[1] = 15;
      length[2] = 30;
         
      new DecimalFormat("#,###.00");
        
      //Labels
      principallb = new JLabel("Principal:");
      interestlb = new JLabel("Interest:");
      termlb = new JLabel("Term:");
      paymentlb = new JLabel("Payment:");
        
      //Text Field
      principalTxFld = new JTextField("", 10);
      interestTxFld = new JTextField("", 5);
      interestTxFld.setEditable(true);
      termTxFld = new JTextField("", 5);
      payment = new JTextField("", 10);
      payment.setEditable(true);
         
      // Pull down table
      IntInterest = new JComboBox(comboItems);
      IntInterest.setEditable(true);
      payAmortTA = new TextArea(20, 40);
        
      //Buttons
      calBtn = new JButton("Calculate");
      calBtn.addActionListener(this);
      resetBtn = new JButton("Reset");
      resetBtn.addActionListener(this);
         
      //Adding the Labels and Fields
      add(principallb);
      add(principalTxFld);
      add(interestlb);
      add(interestTxFld);
      add(termlb);
      add(termTxFld);
      add(paymentlb);
      add(paymentTxFld);
      add(payAmortTA);
      add(calBtn);
      add(resetBtn);
       
 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      pack();
 
      setVisible(true);
       }
 
      public void actionPerformed(ActionEvent e)
      {
    	int principal;
		int intMonthlyRate;
		// calculator
    	  payment=(principal*(intMonthlyRate/12)) / (1-1/Math.Pow((1+interest/12), MonthTerm));
    	  }

    	  {
    	  TextComponent monthlyPayment;
		NumberFormat two_decimal;
		monthlyPayment.setText(""+(two_decimal.format(payment)));

    	  payAmortTA.append("Month No.\tMonthly Payment\t\tLoan Balance\t\tInterest Payment\n");

    	  	Object temp;
			int intMonthlyInterest;
			for (int counter=0; counter < MonthTerm; counter++)

    	  		temp=(1-1/Math.pow((1+intMonthlyInterest/12),Months));
    	  		
    	  		Object InterestPaid = payment * temp;
    	  		JTextField principal;
				Object principle = principal - payment + InterestPaid; {

    	  		MonthTerm = MonthTerm--;
    	  		Months--;
    	  		
    	  		int counter;
				payAmortTA.append((counter + 1) + "\t\t" + (two_decimal.format(payment) + "\t\t"+(two_decimal.format(principal) + "\t\t" + (two_decimal.format(InterestPaid) + "\n"))));
      }
      }
       }

Not sure what lines you are referring to with your question about +/-/* not being defined, but I would strongly guess that it means you are trying to do an arithmetic operation on two operands where at least one of those operands isn't a number. You may be adding a number to a String or a number to a TexBox or a number to a JButton or whatever. Again, I don't know which line you are referring to.

I did notice this:

int principal;
		int intMonthlyRate;
		// calculator
    	  payment=(principal*(intMonthlyRate/12)) / (1-1/Math.Pow((1+interest/12), MonthTerm));

principal and intMonthlyRate are both uninitialized, so you can't use them in a calculation. Presumably you are supposed to get them from the TextBoxes. However, you never do:

int principal = Integer.parseInt (principalTxFld.getText ());
int intMonthlyRate = Integer.parseInt (interestlTxFld.getText ());

Don't name your class "main". "main" is a function and it always has the words "public static void" in front of it. If you want to name your class "Main", that's fine, but don't name it "main". "main" is a public static void function, not a class name, not a variable name.

You have type mismatches all over the place. Arithmetic operations and comparisons involve two numbers. Lines like this don't make sense:

for (int counter = 0; counter < MonthTerm; counter++)

What's MonthTerm? You define it here:

private static final String MonthTerm = null;

I'll reiterate. Try not to use modifiers like "static" and "final" till you're a little more experienced. That said, regardless, MonthTerm is a String and you're comparing it to an integer. It doesn't make sense.

Same here:

MonthTerm = MonthTerm--;

Subtracting 1 from a String doesn't make sense. It's like adding a liter to a kilogram or increasing the temperature by one centimeter. It's a complete mismatch of terms.

Months--;

Again, regarding the modifier "final". You can't change "Months". if you want to, drop the word "final".

private static final double Months = 0;
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.