0

I am trying to get a code that takes a csv file and puts it into an ArrayList and shows the content of the ArrayList. I believe I have the code correct for getting the data into the ArrayList but I can't get it to print. Please any advice is welcome. I am very new to Java and coding in general.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

class Employee{
    public String FirstName;
    public String LastName;
    public String Company;
    public String Address;
    public String City;
    public String County;
    public String State;
    public String Zip;
    public String Phone;
    public String Fax;
    public String Email;
    public String Web;    
}       

public class ITCO321IPWeek4 {
    public static void main(String[] args) throws FileNotFoundException, IOException {

        String line = "";
        ArrayList <Employee> ALEmployee = new ArrayList();

        FileReader fr = new FileReader("C:\\Users\\User\\Downloads\\ITCO321_U4IP_sample_data.csv");
        try (BufferedReader br = new BufferedReader(fr)) {
            while ((line = br.readLine())!=null){ 
                Employee emp = new Employee();
                String[] empFields = line.split(",");
                emp.FirstName = empFields[0];
                emp.LastName = empFields[1];
                emp.Company = empFields[2];
                emp.Address = empFields[3];
                emp.City = empFields[4];
                emp.County = empFields[5];
                emp.State = empFields[6];
                emp.Zip = empFields[7];
                emp.Phone = empFields[8];
                emp.Fax = empFields[9];
                emp.Email = empFields[10];
                emp.Web = empFields[11];
                ALEmployee.add(emp);
            }
        }
    }
} 
5
  • You don’t appear to be printing out the ArrayList anywhere. Commented Nov 20, 2018 at 23:13
  • That is what I am trying to figure out how to do. Commented Nov 20, 2018 at 23:14
  • Using System.out.println()? Commented Nov 20, 2018 at 23:15
  • 1
    Possible duplicate of How to print out all the elements of a List in Java? Commented Nov 20, 2018 at 23:48
  • Yes, I was planning on using the System.out.println(). I was just running into issues on what to put in the (). Commented Nov 20, 2018 at 23:51

2 Answers 2

1

To print an ArrayList you can just

  • Use System.out.println(ALEmployee);

  • or be more precise and print each element one by one

    for(Employee e : ALEmployee){
        System.out.println(e);
    }
    

In both case you'll need to implement the toString() method in Employee class as :

public String toString(){
    return FirstName+" "+LastName+" "+Company; // do whatever you want here
}

Also I would suggest you, to follow Java conventions and coding conventions to:

  • use lowerCamelCase for naming the attributs/variables
  • set the attributs as private and use setters or a constructor to instanciate an Employee with its attributs
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This was very helpful!
0

This is the code I came up with. I would be open for suggestions and critiques on how to clean this up.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
class Employee {
        public String FirstName;
        public String LastName;
        public String Company;
        public String Address;
        public String City;
        public String County;
        public String State;
        public String Zip;
        public String Phone;
        public String Fax;
        public String Email;
        public String Web;

public String toString(){
return "FirstName+" "+LastName+" "+Company+" "+Address+" "+City+""+County+"
        +State+" "+Zip+" "+Phone+" "+Phone+" "+Fax+" "+Email+" "+Web;

}
}       
/**
*
* @author 
*/
public class  {
/**
 * @param args the command line arguments
 * @throws java.io.FileNotFoundException
 */
public static void main(String[] args) throws FileNotFoundException, IOException {

         String line = "";
         ArrayList <Employee> ALEmployee = new ArrayList<Employee>();

    FileReader fr = new FileReader("C:\\Users\\User\\Downloads\\ITCO321_U4IP_sample_data.csv");
    try (BufferedReader br = new BufferedReader(fr)) {
        while ((line = br.readLine())!=null){ 
            Employee emp = new Employee();
            String[] empFields = line.split(",");
            emp.FirstName = empFields[0];
            emp.LastName = empFields[1];
            emp.Company = empFields[2];
            emp.Address = empFields[3];
            emp.City = empFields[4];
            emp.County = empFields[5];
            emp.State = empFields[6];
            emp.Zip = empFields[7];
            emp.Phone = empFields[8];
            emp.Fax = empFields[9];
            emp.Email = empFields[10];
            emp.Web = empFields[11];
        ALEmployee.add(emp);          
    }        
    }
    ALEmployee.forEach((e) -> {
        System.out.println(e);
    });


    }
 }

1 Comment

I know this was not formatted using lower camel case, so sorry for that...

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.