1

I am new to java and trying to work on ArrayList. From Main method I want to >pass the ArrayList to another class CustomerAcc. I need help how to pass the >ArrayList and then how to get the ArrayList in the class Customer code here

import java.io.*;
import java.util.*;
import java.lang.Object;

public class CustomerMain {
    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<String> custName = new ArrayList<String>();
        ArrayList<String> custZip = new ArrayList<String>();
        double count = 0;
        boolean done = false;
        System.out.print("Please enter Customer name or 'X' to exit: ");

        Scanner in = new Scanner(System.in);

        // Gets user input for name and zip code             
        while (in.hasNext() && !done) {
            String checkInput = in.next();

            if (checkInput.equalsIgnoreCase("X")) {
                done = true;
            }
            else {
                custName.add(checkInput);

                System.out.print("Please enter Zip code: ");
                custZip.add(in.next());
                count++;

                System.out.print("\nPlease enter Customer Name or 'X' to `enter code here`exit: ");
            }
        }
        //Sending the user input data to CustomerAcc class
        CustomerAcc custRecord = new CustomerAcc(custName, custZip, count);
    }
}      

The CustomerAcc class :

public class CustomerAcc extends CustomerMain {

   public static void CustomerAcc(String custName, String custZip, int count){

   }
}
1
  • 1
    Please format your question correctly. Commented Jul 4, 2015 at 18:01

3 Answers 3

1

If I understand well that you want to access an ArrayList in another class? if in the case, you can use the getter and setter technique while declaring private ArrayList variables.

private ArrayList<String> custName = new ArrayList<String>();  
private ArrayList<String> custZip = new ArrayList<String>();  



public void setCustName(ArrayList custName){
    this.custName = custName;
}

public ArrayList geCustName(){
    return custName;
}


public void setCustZip(ArrayList custZip){
    this.custZip = custZip;
}

public ArrayList geCustZip(){
    return custZip;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Change constructor of your CustomerAcc class to:

 public CustomerAcc(ArrayList<String> custName, ArrayList<String> custZip, int count){

}

Also you have defined your custName and custZip as

     ArrayList custName = new ArrayList();
     ArrayList custZip = new ArrayList();

which is incorrect. You have to specify type of objects in your ArrayList (I suppose they are strings now.)

Define it like:

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

Comments

0

Syntax while speaking you can change your constructor code definition to -

public CustomerAcc(ArrayList<String> custName, ArrayList<String> custZip, int count){

}

Looking at your code, you could have used Map<String, String> for storing username and its zip, instead of using two lists.

On a separate note, Are you sure you're building single CustomerAcc object taking list of customers and their zipcodes as input?

1 Comment

Hi Raman, I am working on my college project where I have customers first name, last name and the zip code. Then I have to write the info into the file. Before I could start on the project work I was trying to get information into the ArrayList.

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.