fantom
fantom

Reputation: 864

Cannot find items from ArrayList?

I cannot seem to get my saved accounts stored inside my ArrayList. I think the latest one overwrites the oldest one, but I need them all to stack up in the ArrayList.

When I run the program, I create (add to ArrayList) a new Account, say Greg. Then I go on using that account and later on, add another account, say Bob. Then I click on "search for account" and type in Greg, it doesn't find it, but it finds Bob. HOWEVER, if I search for Greg BEFORE I create Bob, it FINDS IT!

Here are the relevant parts of my code:

////////// DECLARATIONS /////////
public static ArrayList dataStore;
public static int       index;
dataStore = new ArrayList(10);
index     = 0;

/////////////////////////////// ADD NEW ACCOUNT ////////////////////////////////
    else if(source.equals("Add new account")) {
//*************         Output
        accountName = JOptionPane.showInputDialog("Enter the account name: ");
        account.setName(accountName);

        String strInitialBalance = JOptionPane.showInputDialog("Enter your initial
        balance: ");
        initialBalance = Double.parseDouble(strInitialBalance);
        account.setBalance(initialBalance);

        account = new CheckingAccount(initialBalance, accountName);
        dataStore.add(index++, account);

    }

//////////////////////////////// FIND ACCOUNT /////////////////////////////////
    else if(source.equals("Find an account")) {
        String str, name;

        str = JOptionPane.showInputDialog("Enter the Account name: ");

        for (int index = 0; index < dataStore.size(); index++) {
            Account datum = (Account)dataStore.get(index);

            if (str.equals(datum.getName())) {
                name = datum.getName();
                textArea.setText("Found Account for " + name);
            }
            else
                textArea.setText("No Accounts found!");

        } // for

    }

This "account" object they are referring to is a class I have where all the data gets stored. Here is its header:

public class CheckingAccount extends Account implements Serializable {
// ...
// Methods
// ...
}

Upvotes: 0

Views: 79

Answers (1)

Josh Engelsma
Josh Engelsma

Reputation: 2646

Here is your original chunk of code.

for (int index = 0; index < dataStore.size(); index++) {
        Account datum = (Account)dataStore.get(index);

        if (str.equals(datum.getName())) {
            name = datum.getName();
            textArea.setText("Found Account for " + name);
        }
        else
            textArea.setText("No Accounts found!");

    }

I dont think it makes sense to have the else statement inside your for loop, try the code below instead...

boolean found = false;
for (int index = 0; index < dataStore.size(); index++) {
        Account datum = (Account)dataStore.get(index);

        if (str.equals(datum.getName())) {
            name = datum.getName();
            textArea.setText("Found Account for " + name);
            found = true;
        }
    }
 if(!found){

     textArea.setText("No Accounts found!");
 }

I think that you don't want the else{} inside the for(...) loop because as you navigate through your data structure, many times, your initial if (str.equals(datum.getName())) statement will fail as you have not found the account yet. However, if you eventually find it, you will meet the if(...) condition and tell the user via textArea that you found the account. Then you set a boolean to true that you found it. If you go through the ENTIRE data structure and have not found it, the boolean will still equal false, and you will meet the condition for the second if which is if(!found) after the for loop, and will tell the user that you have searched the entire data structure and have not found the account.

Also, I think your first else if should look more like the code below (I don't know what the method signatures look like for CheckingAccount so some of this is a guess on actual syntax)

else if(source.equals("Add new account")) {
//*************         Output
    //use constructor below if you have a default constructor aka no paramaters
    CheckingAccount account = new CheckingAccount();
    //now that you have object linked to account, you can use setters
    accountName = JOptionPane.showInputDialog("Enter the account name: ");
    //set the name
    account.setName(accountName);

    String strInitialBalance = JOptionPane.showInputDialog("Enter your initial
    balance: ");
    initialBalance = Double.parseDouble(strInitialBalance);
    //set the balance
    account.setBalance(initialBalance);

    //dont make a new object now as you did before..just add the object to your arraylist
    dataStore.add(index++, account);

}

Upvotes: 2

Related Questions