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);
            }
        }
    }
} 
NewCoder04
  • 19
  • 8

2 Answers2

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
azro
  • 53,056
  • 7
  • 34
  • 70
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);
    });


    }
 }
NewCoder04
  • 19
  • 8