1

I've successfully created a 2D Arraylist but I still am having issues storing my values into the Arraylist. My error message is as follows, "cannot find symbol - method add(int,java.lang.String). I know its probably a simple fix but I couldn't find it online or anywhere in my textbook. And I also am wondering if there is a simpler way to create a 2D arraylist. Thanks.

Here is where I declare the 2D array:

ArrayList <ArrayList<String>> account = new ArrayList<ArrayList<String>>();

Here is my code:

public void newAccount()
{
    firstName = JOptionPane.showInputDialog("What's your first name?");
    nLastName = JOptionPane.showInputDialog("What's your last name?");
    nAddress = JOptionPane.showInputDialog("What's your current address?");
    nCity= JOptionPane.showInputDialog("What's your current city?");
    nState = JOptionPane.showInputDialog("What's your current State?");
    nZipCode = JOptionPane.showInputDialog("What's your current Zip Code?");
    account.add( accountNumber, firstName);
    account.add( accountNumber, nLastName);
    account.add( accountNumber, nAddress);
    account.add( accountNumber, nCity);
    account.add( accountNumber, nState);
    account.add(accountNumber, nZipCode);
0

3 Answers 3

1

Better way is to use object instead of Strings:

ArrayList <Account> account = new ArrayList<Account>();

And this is the Account class:

public class Account{
    public String firstName;
    public String nLastName;
    public String nAddress;
    public String nCity;
    public String nState;
    public String nZipCode; 
}

And addition to list:

public void newAccount()
{
    Account a = new Account();

    a.firstName = JOptionPane.showInputDialog("What's your first name?");
    a.nLastName = JOptionPane.showInputDialog("What's your last name?");
    a.nAddress = JOptionPane.showInputDialog("What's your current address?");
    a.nCity= JOptionPane.showInputDialog("What's your current city?");
    a.nState = JOptionPane.showInputDialog("What's your current State?");
    a.nZipCode = JOptionPane.showInputDialog("What's your current Zip Code?");

    account.add(a);
}

You can change visibility and use it with setter/getter. This is just to explain you the example.

Sign up to request clarification or add additional context in comments.

3 Comments

I have a loop so I can create as many accounts as I would like, would this code overlap the previous account?
No. You add each time a new one to account. Also a is valid only in called method. If you call it multiple times, then you have multiple objects, that will be added to account. Try it out :)
Thank you so much, you probably saved me another 4 hours of looking on the web :)
0

You should first create an ArrayList<String> and add your Strings to this ArrayList.Then add this ArrayList to your account ArrayList.

Comments

0

You dont want an array list of strings, you want an array list of Account Information, like so:

 class AccountEntry{
    public String firstName;
    public String lastName;
    public int address, city, state, zipCode;
 }

 ArrayList<AccountEntry> accounts = new ArrayList<AccountEntry>();

 AccountEntry entry = new AccountEntry();
 entry.firstName = JOptionPane.showInputDialog("What's your first name?");
 //..and so on
 accounts.add( entry ); //add the account

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.