?
Solved

Java string arrayList accessing through program

Posted on 2011-05-06
4
Medium Priority
?
425 Views
Last Modified: 2012-06-21
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
Comment
Question by:marchbaby
  • 3
4 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35706272
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
 
LVL 47

Expert Comment

by:for_yan
ID: 35706288
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
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 35706667

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
 

Author Closing Comment

by:marchbaby
ID: 35706936
I agree that I'm doing this in a really weird way. I'm trying to rework it now.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month16 days, 4 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question