Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Printing an ArrayList of user-defined Objects

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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:
    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:
    	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:
    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


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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:
    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.
    Last edited by Sean4u; March 2nd, 2012 at 04:15 AM.

  3. The Following User Says Thank You to Sean4u For This Useful Post:

    er1111 (March 2nd, 2012)

  4. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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:
    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]);
      }
    }
    Last edited by er1111; March 2nd, 2012 at 02:06 PM.

Similar Threads

  1. Bubblesort of an ArrayList with objects
    By bondage in forum Algorithms & Recursion
    Replies: 9
    Last Post: January 4th, 2012, 11:51 AM
  2. User-Defined Methods
    By ZippyShannon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 28th, 2011, 11:23 PM
  3. User creates objects. How to?
    By Sputnik in forum Object Oriented Programming
    Replies: 5
    Last Post: December 2nd, 2010, 11:18 AM
  4. Searching and printing string results from an Arraylist. Having difficulty.
    By Espressoul in forum Loops & Control Statements
    Replies: 1
    Last Post: February 25th, 2010, 08:32 PM
  5. User Defined Methods
    By mgutierrez19 in forum Object Oriented Programming
    Replies: 11
    Last Post: October 20th, 2009, 07:57 PM