Getting certain String from Arraylist

I have made a Loan GUI that will get someones information and save it in the LoanFolder Array. The problem I am having is with getting a certrain string when asked for it by the GUI.

Here is my code for loanfolder:

import java.util.ArrayList;

public class LoanFolder

{

private ArrayList<Loan> folder;

//--

// Constructor: Creates an initially empty Loan Array

//--

public LoanFolder()

{

ArrayList folder = new ArrayList();

}

public void addLoan (Loan e)

{

folder.add(e);

}

public Loan getLoan (Loan e)

{

for(int i = 0; i <folder.size(); i++)

{

}

Code for the LoanEntry

import java.awt.*;

import java.awt.event.*;

import javax.swing.* ;

public class LoanEntry extends JPanel

{

// Creates buttons, input variables, and text field variables.

private JButton save, retrieve, exit;

private JLabel inputName, inputRate, inputInitialBalance, inputLoanLength;

private JTextField name, rate, initialBalance, loanLength;

private Loan loan;

private LoanFolder folder = new LoanFolder();

// Constructor: Sets up the GUI

public LoanEntry ()

{

inputName = new JLabel ("Name: ");

inputRate = new JLabel ("Rate: ");

inputInitialBalance = new JLabel ("Initial Balance: ");

inputLoanLength = new JLabel ("Loan length in months: ");

name = new JTextField (25);

rate = new JTextField (26);

initialBalance = new JTextField (21);

loanLength = new JTextField (16);

save = new JButton ("Save");

save.addActionListener (new SaveButtonListener());

retrieve = new JButton ("Retrieve");

retrieve.addActionListener (new RetrieveButtonListener());

exit = new JButton ("Exit");

exit.addActionListener (new ExitButtonListener());

add (inputName);

add (name);

add (inputRate);

add (rate);

add (inputInitialBalance);

add (initialBalance);

add (inputLoanLength);

add (loanLength);

add (save);

add (retrieve);

add (exit);

setPreferredSize (new Dimension(350, 150));

}

private class SaveButtonListener implements ActionListener

{

public void actionPerformed (ActionEvent event)

{

String borrowerName;

double loanRate, loanInitialBalance;

int length;

String text = name.getText();

borrowerName = text;

name.setText("");

text = rate.getText();

loanRate = Double.parseDouble (text);

rate.setText("");

text = initialBalance.getText();

loanInitialBalance = Double.parseDouble (text);

initialBalance.setText("");

text = loanLength.getText();

length = Integer.parseInt (text);

loanLength.setText("");

loan = new Loan (borrowerName, loanRate, loanInitialBalance, length);

folder.addLoan (loan);

}

}

private class RetrieveButtonListener implements ActionListener

{

public void actionPerformed (ActionEvent event)

{

String retrieveName, retrieveRate, retrieveInitialBalance, retrieveLength;

try

{

retrieveName = name.getText();

loan = folder.get(retrieveName);

retrieveRate = Double.toString(loan.getRate());

retrieveInitialBalance = Double.toString(loan.getInitialBalance());

retrieveLength = Integer.toString(loan.getLength());

rate.setText(retrieveRate);

initialBalance.setText(retrieveInitialBalance);

loanLength.setText(retrieveLength);

}

catch (NullPointerException e)// catches error of a null name

{

name.setText("Not available");

rate.setText("Not available");

initialBalance.setText("Not available");

loanLength.setText ("Not available");

}

}

}

private class ExitButtonListener implements ActionListener

{

public void actionPerformed (ActionEvent event)

{

System.exit(0); // exit program

}

}

}

Im having trouble figuring out the loop to get the right string back.

Any help would be great Thanks

If you need anything else just let me know.

Message was edited by:

teckademic>

[4302 byte] By [teckademica] at [2007-11-26 22:51:01]
# 1
first off, your LoanFolder is going to give you a nullpointer exception because in the constructor, your are never initializing your arraylist. You are just creating one in a different scope which is garbage collected.Use Code Tags.
Nethera at 2007-7-10 12:12:44 > top of Java-index,Java Essentials,Java Programming...
# 2

hm, I am following the instructions and examples, Ill post all my files

Loan.java

public class Loan extends java.lang.Object

{

private java.lang.String name;

private double rate, initialBalance;

private int length;

// Constructor: creates a new Loan object.

public Loan (java.lang.String name0, double rate0, double initialBalance0, int length0)

{

name = name0;

rate = rate0;

initialBalance = initialBalance0;

length = length0;

}

public double getInitialBalance()

{

return initialBalance;

}

public int getLength()

{

return length;

}

public java.lang.String getName()

{

return name;

}

public double getRate()

{

return rate;

}

}

LoanEntryPanel.java

import javax.swing.JFrame;

public class LoanEntryPanel

{

public static void main (String[] args)

{

JFrame frame = new JFrame ("Loan");

frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

// Creates a LoanEntryobject

frame.getContentPane().add(new LoanEntry());

frame.pack();

frame.setVisible(true);

LoanEntry.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.* ;

public class LoanEntry extends JPanel

{

// Creates buttons, input variables, and text field variables.

private JButton save, retrieve, exit;

private JLabel inputName, inputRate, inputInitialBalance, inputLoanLength;

private JTextField name, rate, initialBalance, loanLength;

private Loan loan;

private LoanFolder folder = new LoanFolder();

// Constructor: Sets up the GUI with the text fields blank.

public LoanEntry ()

{

inputName = new JLabel ("Name: ");

inputRate = new JLabel ("Rate: ");

inputInitialBalance = new JLabel ("Initial Balance: ");

inputLoanLength = new JLabel ("Loan length in months: ");

name = new JTextField (25);

rate = new JTextField (26);

initialBalance = new JTextField (21);

loanLength = new JTextField (16);

save = new JButton ("Save");

save.addActionListener (new SaveButtonListener());

retrieve = new JButton ("Retrieve");

retrieve.addActionListener (new RetrieveButtonListener());

exit = new JButton ("Exit");

exit.addActionListener (new ExitButtonListener());

add (inputName);

add (name);

add (inputRate);

add (rate);

add (inputInitialBalance);

add (initialBalance);

add (inputLoanLength);

add (loanLength);

add (save);

add (retrieve);

add (exit);

setPreferredSize (new Dimension(350, 150));

}

private class SaveButtonListener implements ActionListener

{

public void actionPerformed (ActionEvent event)

{

String borrowerName;

double loanRate, loanInitialBalance;

int length;

String text = name.getText();

borrowerName = text;

name.setText("");

text = rate.getText();

loanRate = Double.parseDouble (text);

rate.setText("");

text = initialBalance.getText();

loanInitialBalance = Double.parseDouble (text);

initialBalance.setText("");

text = loanLength.getText();

length = Integer.parseInt (text);

loanLength.setText("");

loan = new Loan (borrowerName, loanRate, loanInitialBalance, length);

folder.addLoan (loan);

}

}

private class RetrieveButtonListener implements ActionListener

{

public void actionPerformed (ActionEvent event)

{

String retrieveName, retrieveRate, retrieveInitialBalance, retrieveLength;

try

{

retrieveName = name.getText();

loan = folder.get(retrieveName);

retrieveRate = Double.toString(loan.getRate());

retrieveInitialBalance = Double.toString(loan.getInitialBalance());

retrieveLength = Integer.toString(loan.getLength());

rate.setText(retrieveRate);

initialBalance.setText(retrieveInitialBalance);

loanLength.setText(retrieveLength);

}

catch (NullPointerException e)// catches error of a null name

{

name.setText("Not available");

rate.setText("Not available");

initialBalance.setText("Not available");

loanLength.setText ("Not available");

}

}

}

private class ExitButtonListener implements ActionListener

{

public void actionPerformed (ActionEvent event)

{

System.exit(0); // exit program

}

}

}

LoanFolder.java

import java.util.ArrayList;

public class LoanFolder

{

private ArrayList<Loan> folder;

//--

// Constructor: Creates an initially empty Loan Array

//--

public LoanFolder()

{

ArrayList folder = new ArrayList();

}

public void addLoan (Loan e)

{

folder.add(e);

//folder.put(e.getName(), e);

}

public Loan getLoan (Loan e)

{

for(int i = 0; i <folder.size(); i++)

{

}

}

We were given the everything but the loanfolder. I have been stuck for couple days now, so I thought I would see if somone had a little hint or suggestion. Its blowing my mind.

Thanks>

teckademica at 2007-7-10 12:12:44 > top of Java-index,Java Essentials,Java Programming...
# 3
this method does not make any sense:public Loan getLoan(Loan loan)you are given as a parameter the loan that you are looking for?
Nethera at 2007-7-10 12:12:44 > top of Java-index,Java Essentials,Java Programming...
# 4
Sorry to stray off, I see where you are going. Is the add loan correct?
teckademica at 2007-7-10 12:12:44 > top of Java-index,Java Essentials,Java Programming...
# 5
yes, that method is correct.
Nethera at 2007-7-10 12:12:44 > top of Java-index,Java Essentials,Java Programming...
# 6

Ok so when in the LonEntry, when someone press retrieve it does retrieveName = name.getText(); and then loan = folder.get(retrieveName);

In the LoanFolder I need to get that entry from the ArrayList.

The get Method uses the index number to "Get" the correct Array. So i need to do a for loop to check each array to see if it is the correct Array. Then I am confused how to test each array.

public Loan getLoan (Loan e) is wrong because i need to get String Name?

teckademica at 2007-7-10 12:12:44 > top of Java-index,Java Essentials,Java Programming...
# 7

> Ok so when in the LonEntry, when someone press

> retrieve it does retrieveName = name.getText(); and

> then loan = folder.get(retrieveName);

>

> In the LoanFolder I need to get that entry from the

> ArrayList.

>

> The get Method uses the index number to "Get" the

> correct Array. So i need to do a for loop to check

> each array to see if it is the correct Array. Then I

> am confused how to test each array.

>

> public Loan getLoan (Loan e) is wrong because i need

> to get String Name?

I don't know what the requirements of your class are, but it might be something like

public Loan getLoan(String name){

for(Loan loan : arrayOfLoans){

loan.getName().equals(name)

return loan;

}

}

Nethera at 2007-7-10 12:12:44 > top of Java-index,Java Essentials,Java Programming...
# 8

>I don't know what the requirements of your class are, but it might be >something like

>

>

>public Loan getLoan(String name){

>for(Loan loan : arrayOfLoans){

>loan.getName().equals(name)

>return loan;

>}

>}

Thanks for your help. Im starting to see how this works.

For the loan.getName().euals(name) do I need a ; after? If I dont it gives me the error

LoanFolder.java:34: ';' expected

return loan;

But when I add the ; after .equals(name) error

LonFolder.java:31: cannot find symbol

symbol : variable arrayOfLoans

location: class LoanFolder

for(Loan loan : arrayOfLoans)

Seems like its saying arrayOfLoans is a varable, do I need to set it to somthing?

Thanks you so much, kind person.

teckademica at 2007-7-10 12:12:44 > top of Java-index,Java Essentials,Java Programming...
# 9
arrayOfLoans is what you call "folder"and yes, there is supposed to be a semicolon.
Nethera at 2007-7-10 12:12:44 > top of Java-index,Java Essentials,Java Programming...
# 10
Think im getting close. Isnt the return the last code the method uses. So when it does the for loop, and does get name = to name and does the return loan; thats the end of the loop, but not the end of the methodits going to expect a return statment
teckademica at 2007-7-10 12:12:44 > top of Java-index,Java Essentials,Java Programming...