Printing an ArrayList of user-defined Objects
Hi guys, I'm having trouble printing an ArrayList of Employees(My objects) for school. It keeps printing out what I think to be the hexcodes for various characters. I tried many variants of google searches but none of the answers that came up seemed to help.
Here is my code:
Code Java:
import java.util.*;
import java.io.*;
public class MyClass
{
public static void main (String [] args)
{
ArrayList<Employee> list = loadEmployees();
for (int i=0;i<list.size();i++) //will show what this prints below
{
System.out.println(list.get(i).printerString());
}
}
static ArrayList loadEmployees()
{
ArrayList<String[]> list = new ArrayList<String[]>();
ArrayList<Employee> employees = new ArrayList<Employee>();
BufferedReader br = null;
String [] strArr, arr;
Object [] objArr = new Object[5];
String line;
int count = 0;
try
{
br = new BufferedReader(new FileReader("MyFilePath"));
strArr = new String[10];
while ((line = br.readLine()) != null)
{
strArr = line.split("\\s+");
list.add(count, strArr);
count++;
}
}
catch(Exception e){}
for (int i=0; i<list.size(); i++) //I think that this for-loop may be part of the problem, basically its filling ArrayList employees with objects
{
list.toArray(objArr);
arr = new String[objArr.length];
for(int g=0; g<arr.length;g++)
{
arr[g] = objArr[g].toString();
}
String temp = Arrays.toString(arr);
employees.add(i, Employee.create(temp));
}
return employees;
}
}
class Employee
{
String name, company, projectName, projectPosition;
private Employee(String name, String company, String projectName, String projectPosition)
{
this.name = name;
this.company = company;
this.projectName = projectName;
this.projectPosition = projectPosition;
}
public String printerString()
{
String str = "Name: " + name + "\nCompany: " + company + "\nProject Name" + projectName + "\nProject Position: " + projectPosition + "\n";
return str;
}
public static Employee create(String str)
{
String[] holder = str.split("\\s+");
return new Employee(holder[0], holder[1], holder[2], holder[3]);
}
}
Not sure if needed, but here is the file I'm reading from:
Code =java:
Joe EISI Project1 manager
Fred RIM Project3 developer
Sally EISI Project2 developer
Jane RIM Project3 qualityAssurance
Molly EA Project2 graphicDesign
It is a .txt file.
My output looks like this:
Code =java:
Name: [[Ljava.lang.String;@207148e9,
Company: [Ljava.lang.String;@6d69c9a2,
Project Name[Ljava.lang.String;@3c34e2cc,
Project Position: [Ljava.lang.String;@3415ddf5,
Name: [[Ljava.lang.String;@207148e9,
Company: [Ljava.lang.String;@6d69c9a2,
Project Name[Ljava.lang.String;@3c34e2cc,
Project Position: [Ljava.lang.String;@3415ddf5,
Name: [[Ljava.lang.String;@207148e9,
Company: [Ljava.lang.String;@6d69c9a2,
Project Name[Ljava.lang.String;@3c34e2cc,
Project Position: [Ljava.lang.String;@3415ddf5,
Name: [[Ljava.lang.String;@207148e9,
Company: [Ljava.lang.String;@6d69c9a2,
Project Name[Ljava.lang.String;@3c34e2cc,
Project Position: [Ljava.lang.String;@3415ddf5,
Name: [[Ljava.lang.String;@207148e9,
Company: [Ljava.lang.String;@6d69c9a2,
Project Name[Ljava.lang.String;@3c34e2cc,
Project Position: [Ljava.lang.String;@3415ddf5,
I think that's everything, let me know if you need to know anything else.
Thanks,
er1111
Re: Printing an ArrayList of user-defined Objects
What you're seeing is the output from Object.toString() - read the API documentation for the method, and then override it in your own classes. Writing toString() methods for my classes is one of my favourite Java programming activities: it's like putting the cherry on the cake.
Glancing at your code again, you may have more problems than that. If you have a list of Employee, or even a list of lists of Employee, all you should need to do is to implement Employee.toString() and then:
Code java:
List<List<Employee>> listOfListsOfEmployees = makeWeirdEmployeeListOfLists();
System.out.println(listOfListsOfEmployees)
List.toString will iterate over its members invoking toString() on each, concatenate all the return values nicely and return it.
Re: Printing an ArrayList of user-defined Objects
Hi there! I did have a.toString() method, but I found that I needed the original.toString() method from Arrays.toString(), so I ended up just calling my .tostring() method printerstring.
For some reason when i called Arrays.toString() while my Employee.toString() method existed, it produced a different result, like I had somehow edited Arrays.toString() by creating Employee.toString(). I changed my Employee.toString() method to Employee.printerString();
-EDIT-
typos
-EDIT 2-
I ended up just messing around with my loadEmployees() method and my Employee.create method, cleaned it up and it ended up working. Here is the (semi-)finished code:
Code =java:
import java.util.*;
import java.io.*;
@SuppressWarnings("unchecked")
public class MyClass
{
public static void main (String [] args)
{
ArrayList<Employee> list = loadEmployees();
for (int i=0;i<list.size();i++)
{
System.out.println(list.get(i).printerString());
}
}
public static void processEmployees()
{
ArrayList<Employee> employees = loadEmployees();
printEmployees(employees);
}
@SuppressWarnings("unchecked")
static ArrayList loadEmployees()
{
ArrayList<String> list = new ArrayList<String>();
ArrayList<Employee> employees = new ArrayList<Employee>();
BufferedReader br = null;
String [] strArr, arr = new String[5];
String line;
int count = 0;
try
{
br = new BufferedReader(new FileReader("MyFilePath"));
strArr = new String[10];
while ((line = br.readLine()) != null)
{
strArr = line.split("\\s+");
employees.add(0, Employee.create(strArr)); //just added this part in, rather than that messy forloop
count++;
}
}
catch(Exception e){ System.out.println(e.getMessage()); }
return employees;
}
}
class Employee
{
String name, company, projectName, projectPosition;
private Employee(String name, String company, String projectName, String projectPosition)
{
this.name = name;
this.company = company;
this.projectName = projectName;
this.projectPosition = projectPosition;
}
public String printerString()
{
String str = "Name: " + name + "\nCompany: " + company + "\nProject Name: " + projectName + "\nProject Position: " + projectPosition + "\n";
return str;
}
public static Employee create(String[] str) //changed this method
{
return new Employee(str[1], str[2], str[3], str[4]);
}
}