• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 430
  • Last Modified:

Java string arrayList accessing through program

Hi,

I have created the arraylist mortgage that needs to be added to with the create button, display the added info with the next button. I have constructed the array list but I can't get the buttons to access the array. The errors I am getting are "Cannot find sysmbol; variable mortgage" in the next button and create button area. Do I need to move my arraylist? I've tried and it doesn't reconize it then either.

Thank you in advance!
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package may1st;

/**
 *
 * @author Heather
 */
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.lang.reflect.InvocationTargetException;
import javax.swing.ImageIcon;
import java.lang.String;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


public class Main implements Runnable {


    //The main container for this application
    public JFrame jfrMain;
    public JTextField fnameTextField;
    public JTextField lnameTextField;
    public JTextField phoneTextField;
    public JTextField loanAmountField;
    public JTextField termYearsField;
    public JTextField interestRateField;
    public JTextField monthlyPaymentField;
    public JButton calcButton;
    public JButton nextButton;
    public JButton exitButton;
    public JButton createButton;

    /**
     *
     * @param args Command line arguments (not used)
     */
    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Main());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * Setup the GUI and start the program.
     */
    public void run() {
        jfrMain = new JFrame("Mortgage Calculator");
        jfrMain.setLayout(new GridLayout(8, 2));
        jfrMain.setSize(new Dimension(900, 500));

        //Construct the fields

        fnameTextField = new JTextField (20);
        lnameTextField = new JTextField(20);
        phoneTextField = new JTextField(20);
        loanAmountField = new JTextField(10);
        termYearsField = new JTextField(4);
        interestRateField = new JTextField(2);
        monthlyPaymentField = new JTextField(10);
        monthlyPaymentField.setEditable(false);

  //array construct
        ArrayList mortgage = new ArrayList();
        mortgage.add(fnameTextField);
        mortgage.add(lnameTextField);
        mortgage.add(phoneTextField);
        mortgage.add(loanAmountField);
        mortgage.add(termYearsField);
        mortgage.add(interestRateField);
        mortgage.add(monthlyPaymentField);





        //adding the labels
        jfrMain.add(new JLabel(new ImageIcon("C:/Users/Heather/Documents/UoP/Java_IT215/Week 7/may1st/src/may1st/home.jpg"), JLabel.LEFT));
        jfrMain.add(new JLabel("Heather's Mortage Calculator"));
        jfrMain.add(new JLabel(""));
        jfrMain.add(new JLabel(""));
        jfrMain.add(new JLabel("First Name"));
        jfrMain.add(fnameTextField);
       
        jfrMain.add(new JLabel("Last Name"));
        jfrMain.add(lnameTextField);
     
        jfrMain.add(new JLabel("Phone"));
        jfrMain.add(phoneTextField);
        
        jfrMain.add(new JLabel("Loan Amount"));
        jfrMain.add(loanAmountField);
        jfrMain.add(new JLabel(""));
        jfrMain.add(new JLabel(" Loan Terms in months"));
        jfrMain.add(termYearsField);
        jfrMain.add(new JLabel(""));
        jfrMain.add(new JLabel("Interest Rate"));
        jfrMain.add(interestRateField);
        
        jfrMain.add(new JLabel("Monthly payment"));
        jfrMain.add(monthlyPaymentField);
        jfrMain.add(new JLabel(""));
  
    

       

        //create button
        createButton = new JButton("Create");
        createButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    oncreateButtonClick(e);
                }
            });
        jfrMain.add(createButton);
        jfrMain.setVisible(true);

        //calculate button
        calcButton = new JButton("Calculate");
        calcButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    onCalcButtonClick(e);
                }
            });

        jfrMain.add(calcButton);
        jfrMain.setVisible(true);

        // next button 
        nextButton = new JButton("Next");
        nextButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    onnextButtonClick(e);
                }
            });
        jfrMain.add(nextButton);
        jfrMain.setVisible(true);

        //exit button 
        exitButton = new JButton ("Exit");
        exitButton.addActionListener (new ActionListener() {
        public void actionPerformed(ActionEvent e)
           {
             System.exit(0);
              }
              });
         jfrMain.add(exitButton);
         jfrMain.setVisible(true);
    }

    public void oncreateButtonClick(ActionEvent e){

        mortgage.add(fnameTextField);
        mortgage.add(lnameTextField);
        mortgage.add(phoneTextField);
        mortgage.add(loanAmountField);
        mortgage.add(termYearsField);
        mortgage.add(interestRateField);
        mortgage.add(monthlyPaymentField);
    

    }

    public void onnextButtonClick (ActionEvent e){

        nextButton.setActionCommand ("next");

        fnameTextField.setText(mortgage.getfnameTextField);
	lnameTextField.setText(mortgage.getlnameTextField());
        phoneTextField.setText(mortgage.getphoneTextField());
        loanAmountField.setText(mortgage.getloanAmountField());
        termYearsField.setText(mortgage.termYearsField());
        interestRateField.setText(mortgage.getinterestRate());
        monthlyPaymentField.setText(mortgage.getmonthlyPaymentField());
    }

    public void onCalcButtonClick(ActionEvent e) {


        double loanAmount;
        double termYears;
        double interestRate;

        try {
            loanAmount = Double.parseDouble(loanAmountField.getText());
        } catch (NumberFormatException nf) {
            JOptionPane.showMessageDialog(jfrMain, "Please enter loan amount");

            return;
        }

        try {
            termYears = Double.parseDouble(termYearsField.getText());
        } catch (NumberFormatException nf) {
            JOptionPane.showMessageDialog(jfrMain,
                "Please enter mortgage term length");

            return;
        }

        try {
            interestRate = Double.parseDouble(interestRateField.getText()) / 100;
        } catch (NumberFormatException nf) {
            JOptionPane.showMessageDialog(jfrMain, "Please enter interest rate");

            return;
        }

        //Calculate monthly payment
        double payment = calculatePayment(loanAmount, termYears, interestRate);
        monthlyPaymentField.setText(new Double(payment).toString());
    }

    public double calculatePayment(double loanAmount, double termYears,
        double interestRate) {
        double monthlyInterest = interestRate / 12;
        double monthlyPayment = (double) (loanAmount * (monthlyInterest / (1 -
            Math.pow(1 + monthlyInterest, termYears * -1))));
        monthlyPayment = Math.ceil(monthlyPayment * 100) / 100;

        return monthlyPayment;
    }
}

Open in new window

0
marchbaby
Asked:
marchbaby
  • 3
1 Solution
 
for_yanCommented:
Try to make your arraylist called mortgage instance variablke, declare it just wiuthin your class and then only initialize and fill in inside method run().
Otherwise, what I think happens yoiu create it as inside method and then try to access it in another method
0
 
for_yanCommented:
This is what I see from your snippet; if you don't know how to change I'll work more with you when I reach my compouter
0
 
for_yanCommented:

I modified it below so that it should not complain
about inknown symbol, becuae now mortgage is
an instance variable and should be recognized throughout the class.

In general I have some reservations about your design - it is uncommon to use
textFileds as elements of the ArrayList and I'm not sure if you need to use threads.
All those things are throretically possible, but in my experience with different
versions of motrgage calculteor which is a recurring theme
here on EE, people normally do not do it this way.

But this is a whole different story - at least your method should not complai about
unknown symbol mortgage now
 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package may1st;

/**
 *
 * @author Heather
 */
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.lang.reflect.InvocationTargetException;
import javax.swing.ImageIcon;
import java.lang.String;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


public class Main implements Runnable {


    //The main container for this application
    public JFrame jfrMain;
    public JTextField fnameTextField;
    public JTextField lnameTextField;
    public JTextField phoneTextField;
    public JTextField loanAmountField;
    public JTextField termYearsField;
    public JTextField interestRateField;
    public JTextField monthlyPaymentField;
    public JButton calcButton;
    public JButton nextButton;
    public JButton exitButton;
    public JButton createButton;
ArrayList mortgage;

    /**
     *
     * @param args Command line arguments (not used)
     */
    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Main());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * Setup the GUI and start the program.
     */
    public void run() {
        jfrMain = new JFrame("Mortgage Calculator");
        jfrMain.setLayout(new GridLayout(8, 2));
        jfrMain.setSize(new Dimension(900, 500));

        //Construct the fields

        fnameTextField = new JTextField (20);
        lnameTextField = new JTextField(20);
        phoneTextField = new JTextField(20);
        loanAmountField = new JTextField(10);
        termYearsField = new JTextField(4);
        interestRateField = new JTextField(2);
        monthlyPaymentField = new JTextField(10);
        monthlyPaymentField.setEditable(false);

  //array construct
        mortgage = new ArrayList();
        mortgage.add(fnameTextField);
        mortgage.add(lnameTextField);
        mortgage.add(phoneTextField);
        mortgage.add(loanAmountField);
        mortgage.add(termYearsField);
        mortgage.add(interestRateField);
        mortgage.add(monthlyPaymentField);





        //adding the labels
        jfrMain.add(new JLabel(new ImageIcon("C:/Users/Heather/Documents/UoP/Java_IT215/Week 7/may1st/src/may1st/home.jpg"), JLabel.LEFT));
        jfrMain.add(new JLabel("Heather's Mortage Calculator"));
        jfrMain.add(new JLabel(""));
        jfrMain.add(new JLabel(""));
        jfrMain.add(new JLabel("First Name"));
        jfrMain.add(fnameTextField);
       
        jfrMain.add(new JLabel("Last Name"));
        jfrMain.add(lnameTextField);
     
        jfrMain.add(new JLabel("Phone"));
        jfrMain.add(phoneTextField);
        
        jfrMain.add(new JLabel("Loan Amount"));
        jfrMain.add(loanAmountField);
        jfrMain.add(new JLabel(""));
        jfrMain.add(new JLabel(" Loan Terms in months"));
        jfrMain.add(termYearsField);
        jfrMain.add(new JLabel(""));
        jfrMain.add(new JLabel("Interest Rate"));
        jfrMain.add(interestRateField);
        
        jfrMain.add(new JLabel("Monthly payment"));
        jfrMain.add(monthlyPaymentField);
        jfrMain.add(new JLabel(""));
  
    

       

        //create button
        createButton = new JButton("Create");
        createButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    oncreateButtonClick(e);
                }
            });
        jfrMain.add(createButton);
        jfrMain.setVisible(true);

        //calculate button
        calcButton = new JButton("Calculate");
        calcButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    onCalcButtonClick(e);
                }
            });

        jfrMain.add(calcButton);
        jfrMain.setVisible(true);

        // next button 
        nextButton = new JButton("Next");
        nextButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    onnextButtonClick(e);
                }
            });
        jfrMain.add(nextButton);
        jfrMain.setVisible(true);

        //exit button 
        exitButton = new JButton ("Exit");
        exitButton.addActionListener (new ActionListener() {
        public void actionPerformed(ActionEvent e)
           {
             System.exit(0);
              }
              });
         jfrMain.add(exitButton);
         jfrMain.setVisible(true);
    }

    public void oncreateButtonClick(ActionEvent e){

        mortgage.add(fnameTextField);
        mortgage.add(lnameTextField);
        mortgage.add(phoneTextField);
        mortgage.add(loanAmountField);
        mortgage.add(termYearsField);
        mortgage.add(interestRateField);
        mortgage.add(monthlyPaymentField);
    

    }

    public void onnextButtonClick (ActionEvent e){

        nextButton.setActionCommand ("next");

        fnameTextField.setText(mortgage.getfnameTextField);
	lnameTextField.setText(mortgage.getlnameTextField());
        phoneTextField.setText(mortgage.getphoneTextField());
        loanAmountField.setText(mortgage.getloanAmountField());
        termYearsField.setText(mortgage.termYearsField());
        interestRateField.setText(mortgage.getinterestRate());
        monthlyPaymentField.setText(mortgage.getmonthlyPaymentField());
    }

    public void onCalcButtonClick(ActionEvent e) {


        double loanAmount;
        double termYears;
        double interestRate;

        try {
            loanAmount = Double.parseDouble(loanAmountField.getText());
        } catch (NumberFormatException nf) {
            JOptionPane.showMessageDialog(jfrMain, "Please enter loan amount");

            return;
        }

        try {
            termYears = Double.parseDouble(termYearsField.getText());
        } catch (NumberFormatException nf) {
            JOptionPane.showMessageDialog(jfrMain,
                "Please enter mortgage term length");

            return;
        }

        try {
            interestRate = Double.parseDouble(interestRateField.getText()) / 100;
        } catch (NumberFormatException nf) {
            JOptionPane.showMessageDialog(jfrMain, "Please enter interest rate");

            return;
        }

        //Calculate monthly payment
        double payment = calculatePayment(loanAmount, termYears, interestRate);
        monthlyPaymentField.setText(new Double(payment).toString());
    }

    public double calculatePayment(double loanAmount, double termYears,
        double interestRate) {
        double monthlyInterest = interestRate / 12;
        double monthlyPayment = (double) (loanAmount * (monthlyInterest / (1 -
            Math.pow(1 + monthlyInterest, termYears * -1))));
        monthlyPayment = Math.ceil(monthlyPayment * 100) / 100;

        return monthlyPayment;
    }
}

Open in new window

0
 
marchbabyAuthor Commented:
I agree that I'm doing this in a really weird way. I'm trying to rework it now.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now