Hey guys I am stumped. My code worked fine until I added the calculations:

else{
				 interestRate = rate[choice];
				 termLoan = tYear[choice];
}

:\Users\Mom\Desktop\DuttonWeek4.java:252: incompatible types
found : double
required: javax.swing.JTextField
interestRate = rate[choice];
^
C:\Users\Mom\Desktop\DuttonWeek4.java:253: incompatible types
found : int
required: javax.swing.JTextField
termLoan = tYear[choice];
^
2 errors

Tool completed with exit code 1


I need to use the calculation or something similar can someone help me out here is the rest of my code.

/*

 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;


public class DuttonWeek4 implements ActionListener {

   JTextField loanAmount;    //Loan Amount entered by the user
   JTextField interestRate;  //Interest Rate Amount entered by the user
   JTextField termLoan;	     //The Term (years) entered by the user
   JComboBox loanYears;      //number of years and percentage for the Loan
   JScrollPane sPane;        //Scroll bar that handles the overflow of payment within the table
   JTable t;				 //Table that holds the payment arrays



    private final String[] cols = {"Payment #","Payment","Amount Paid","Interest Paid","Balance"}; //titles for the columns
    private final int NO_OF_COLUMNS = 5;  //number of columns for the table
    final int tMonth = 12;                //number of months for each year


    private final int[]tYear = new int [3];    // intitate tYear
    private double[]rate = new double [3];    //interest rate array
	private double lAmount;						//double for parse and calculation
	private double lRate;						//double for parse and calculation
	private double lTerm;						//double for parse and calculation

   private void init() {       //private method to intitate tYear

   tYear[0] = 7;				// initialize terms
   tYear[1] = 15;				// initialize terms
   tYear[2] = 30;				// initialize terms

   rate[0] = 0.0535;			// initialize interest rate
   rate[1] = 0.0550;			// initialize interest rate
   rate[2] = 0.0575;			// initialize interest rate

   }


   public void calculate(double lPayment, double ratePayment, double termPayment) {	// calculate loan payments  base on user choice

	  int numOfPayments = (int) (termPayment * tMonth);

	  NumberFormat usFormat = NumberFormat.getCurrencyInstance(Locale.US); 	  //Using number format based on Localized currency
      double balance = lPayment; 			  //  static method of the Double class to throw a NumberFormatException.


     Object[][] data = new Object[numOfPayments +1][NO_OF_COLUMNS]; //Objects to be put into the table columns


	double mPayment = (balance * ratePayment / tMonth) /
                            (1 - (Math.pow(1+ (ratePayment / tMonth), (-numOfPayments))));


     double iPaid;               	// varible for interest paid
     double tInterest = 0.0;	   	// varible for total interest
     double tPayment = 0.0;      	// varible for total payment


     for (int i = 0; i < numOfPayments ;  i++) {   //Calculation and initializing of array

     iPaid = balance * ratePayment / tMonth;              //Calculation for interest paid in the  array
     tInterest = tInterest + iPaid;                         //Calculation for the total interest in the array
     tPayment = tPayment + mPayment;                        //Calculation for the total payment in the array
     balance = balance - mPayment + iPaid;                  //Calculation for the Balance

     data[i][0] = ""+(i+1);								    //Adding to the array
     data[i][1] = usFormat.format(mPayment);				//Adding monthly payment data to the array
     data[i][2] = usFormat.format(mPayment - iPaid);		//Calculation and adition to the monthly payment data to the array
     data[i][3] = usFormat.format(iPaid);					//Adding interest paid data to the array
     data[i][4] = usFormat.format(balance);				    //Adding balance data to the array
   }


     data[numOfPayments][0] = "Total Balance";   		    // Calculations for total balance of last row to the array
     data[numOfPayments][1] = usFormat.format(tPayment);   // Calculations for total payment of last row to the array
     data[numOfPayments][2] = "";						    // Calculations for the last row to the array
     data[numOfPayments][3] = usFormat.format(tInterest);  // Calculations for total interst of last row to the array
     data[numOfPayments][4] = "";						    // Calculations for the last row to the array

     t = new JTable(data, cols);									// Displays tables of cells
     sPane.setViewportView(t);									    // Creates scroll bar

}

  public void populatePane(Container cont) {                        //creates and populates the container(window) for the calculator

     cont.setLayout(new GridBagLayout());							//sets the layout for the container(window) for the calculator


     GridBagConstraints c = new GridBagConstraints();				//object constraint for grid(table)

   																    //adds Loan Amount field

     c.fill = GridBagConstraints.HORIZONTAL;       				    // placed the  component horizontally within the display area
     c.gridx = 0;                                  				    // column# 0
     c.gridy = 0;                                  				    // row# 0
     c.insets = new Insets(8,10,5,8);            					// sizing and placement of the lable for the Loan payment
     cont.add(new JLabel("Loan Amount:"), c);        				// add label to pane

     c.fill = GridBagConstraints.HORIZONTAL;					    // placed the  component horizontally within the display area
     c.gridx = 1;                                  				    // column# 1
     c.gridy = 0;                                  				    // row# 0
     c.gridwidth = 2;                              				    // sizes the display area
     loanAmount = new JFormattedTextField("");					    // Formats the text field
     cont.add(loanAmount, c);                       				// sizing and placement of the lable for the Loan payment

  																    // adds the textfield for the user to enter the loan amount
     c.fill = GridBagConstraints.HORIZONTAL;						// placed the  component horizontally within the display area
     c.gridx = 0;                                  				    // column# 0
     c.gridy = 1;                                  				    // row# 1
     c.gridwidth = 1;                              				    // display area occupies one column
     cont.add(new JLabel("Interest Rate:"), c);         // sizing and placement of the label for the ComboBox

	 c.fill = GridBagConstraints.HORIZONTAL;					    // placed the  component horizontally within the display area
     c.gridx = 1;                                  				    // column# 1
     c.gridy = 1;                                  				    // row# 0
     c.gridwidth = 2;                              				    // sizes the display area
     interestRate = new JFormattedTextField("");					    // Formats the text field
     cont.add(interestRate, c);                       				// sizing and placement of the lable for the Loan payment

	 c.fill = GridBagConstraints.HORIZONTAL;						// placed the  component horizontally within the display area
     c.gridx = 0;                                  				    // column# 0
     c.gridy = 2;                                  				    // row# 1
     c.gridwidth = 1;                              				    // display area occupies one column
     cont.add(new JLabel("Term of Loan:"), c);         // sizing and placement of the label for the ComboBox

	 c.fill = GridBagConstraints.HORIZONTAL;					    // placed the  component horizontally within the display area
     c.gridx = 1;                                  				    // column# 1
     c.gridy = 2;                                  				    // row# 0
     c.gridwidth = 2;                              				    // sizes the display area
     termLoan = new JFormattedTextField("");					    // Formats the text field
     cont.add(termLoan, c);                       				// sizing and placement of the lable for the Loan payment

  																	// adds the textfield for the user to enter the loan amount
     c.fill = GridBagConstraints.HORIZONTAL;						// placed the  component horizontally within the display area
     c.gridx = 0;                                  				    // column# 0
     c.gridy = 3;                                  				    // row# 1
     c.gridwidth = 1;                              				    // display area occupies one column
     cont.add(new JLabel("Loan Amount with Interest:"), c);         // sizing and placement of the label for the ComboBox



     loanYears = new JComboBox();										// creates and places the combo box
     loanYears.addItem("7 years at 5.35%");							    // adds item to the combo box
     loanYears.addItem("15 years at 5.5%");							    // adds item to the combo box
     loanYears.addItem("30 years at 5.75%");							    // adds item to the combo box

     c.fill = GridBagConstraints.HORIZONTAL;						// placed the  component horizontally within the display area
     c.gridx = 1;                                                   // column# 1
     c.gridy = 3;                                  				    // row 1
     c.gridwidth = 2;                              				    // make display area as wide as 2 columns
     c.insets = new Insets(5,10,5,185);             				// external padding
     cont.add(loanYears, c);

   																    // creates and places the Calculate button
     c.fill = GridBagConstraints.HORIZONTAL;                        // placed the  component horizontally within the display area
     c.gridx = 0;                                  			        // column# 2
     c.gridy = 4;                                  			        // row# 2
     c.weightx = 0.05;                             			        // set weight for 3rd column
     c.gridwidth = 1;                              			        // display area occupies 1 column
     c.insets = new Insets(5,10,0,20);							    // Places the button
     c.anchor = GridBagConstraints.WEST;							// place the component to the left (within its display area)
     JButton calcButton = new JButton("Calculate");				    // Creates the calculate button
     calcButton.setActionCommand("calculate");    				    // set action command for this button
     calcButton.addActionListener(this);							// listens for the action command for this button
     cont.add(calcButton, c);										// Adds the button to the grid

													        		// creates and places the exit button
     c.fill = GridBagConstraints.HORIZONTAL;         				// placed the  component horizontally within the display area
     c.gridx = 1;                                  				    // column# 1
     c.gridy = 4;                                  				    // row# 2
     c.gridwidth = 1;                              				    // display area 1 column
     c.weightx = 0.95;                             				    // set weight for second column (bigger than third column)
     c.insets = new Insets(5,5,0,200);							    // Places the button
     c.anchor = GridBagConstraints.WEST;      				        // place the component to the left (within its display area)
     JButton exitButton = new JButton("Exit");					    // Creates the exit button
     exitButton.setActionCommand("exit");          				    // set action command for this button
     exitButton.addActionListener(this);							// listens for the action command for this button
     cont.add(exitButton, c);										// Adds the button to the grid

   																    // add the table to the grid
     sPane = new JScrollPane();									    // create and add th scroll bar
     c.fill = GridBagConstraints.HORIZONTAL;						// placed the  component horizontally within the display area
     c.gridx = 0;                                  				    // column# 0
     c.gridy = 5;                                  				    // row# 0
     c.gridwidth = 3;                              				    // display area occupies 3 columns
     c.insets = new Insets(5,10,10,10);							    // Places the button

   																    // adding empty rows and columns for input to the table
     Object[][] dummy = { {"","","","","",""} };					// adding rows to the table
     t = new JTable(dummy, cols);									// adding columns to the table

     sPane.setViewportView(t);									    // setting viewier for the scroll bar to the table
     cont.add(sPane, c);											// adding scroll bar to the table
   }

   public void createAndShowGUI() {									// Creating the GUI

     JFrame f = new JFrame("McBride Mortgage Calculator");			// title for the GUI displayed
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);			// Default exit for the GUI

     																// Creating and placing of the components in the frame of the GUI
     populatePane(f.getContentPane());								// populating the GUI with the ContentPane
       f.pack();
       f.setVisible(true);
   }

   public void actionPerformed(ActionEvent e) {						//using the action performed event to use the buttons

       try {														// the try (or throw)step in checking for NumberFormatException even with

        															// which option user selected for the calculation
         int choice = loanYears.getSelectedIndex();

             if ("calculate".equals(e.getActionCommand())) {		// using the calculation button to calculate the loans
             if (!loanAmount.getText().equals("#,##")){ 				 // calculate when Loan amount  is entered
				lAmount = Double.parseDouble(loanAmount.getText());
				}
             if (!interestRate.getText().equals("#,##") & !!termLoan.getText().equals("#,##")){

		 	 	lRate = Double.parseDouble(interestRate.getText()) / 100;
		 	 	lTerm = Double.parseDouble(termLoan.getText());
		  }

		  	 else{
				 interestRate = rate[choice];
				 termLoan = tYear[choice];
}

                calculate(lAmount, lRate, lTerm);
}
			else if ("clear".equals(e.getActionCommand())){
				loanAmount.setText("");
				interestRate.setText("");
				termLoan.setText("");

   }
             else
				System.exit(0);		// using the exit button with the Action performed event

   }
       catch(NumberFormatException event)  {						// the Catch step in checking for NumberFormatException event


	     JOptionPane.showMessageDialog								// uses a command prompt to tellthe user to use a number without commas
	     (null, "Please enter the loan amount without commas",		// this is the "result" of the Catch step of a NumberFormatException
	     "NumberFormatException", JOptionPane.ERROR_MESSAGE);


	}
    }

   public static void main(String[] args) {

       final DuttonWeek4 dw4 = new DuttonWeek4();
         dw4.init();
         javax.swing.SwingUtilities.invokeLater(new Runnable() {	// makes the runs the javax.swing.SwingUtilities allowing the program to run

   public void run() {
       dw4.createAndShowGUI();										// makes the runs the createAndShowGUI
   }
   });
  }
}

I just can't figure out how to make the double and int work together. I have read a ton of stuff so I am closer to figuring it out but am still a bit confused.

Note if you put my code in notepad instead of TextPad it turns it into a huge block of code. if this is a huge issue let me know and Ill take all the comments out. just haven't had a chance yet.

Thanks
bunifrog

Recommended Answers

All 2 Replies

Member Avatar for coil

interestRate and termLoan are not primitives, they are JTextFields. You can't directly assign a value to JTextFields.

Read the API doc for JTextField for a method to put data into the textfield.

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.