Results 1 to 6 of 6
  1. #1
    Sidmyre is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default [SOLVED] Arrays of Objects with Subscripts

    Hi there, im trying to figure out how to how to display a list of the first names that are in an array of objects with subscripts, as a loop.

    shown below

    Java Code:
    		Employee names[] = new Employee[5];
    
    		names[0].setFirstName("John");
    		names[0].setLastName("doe");
    		names[0].setID("1234");
    
    		names[1].setFirstName("Mary");
    		names[1].setLastName("berry");
    		names[1].setID("2345");
    
    		names[2].setFirstName("Chris");
    		names[2].setLastName("bliss");
    		names[2].setID("3456");
    
    		names[3].setFirstName("Angela");
    		names[3].setLastName("Jolie");
    		names[3].setID("4567");
    
    		names[4].setFirstName("Marky");
    		names[4].setLastName("mark");
    		names[4].setID("5678");
    
    //This is what ive tried but it does not work
    for (int i = 0; i < names.length; i++)
    	 System.out.println(names[i].getFirstName());

    Edit: this accesses another file Employee, with methods already.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Does this Employee class have a toString() method? If so, you might be able to display it quite simply:

    Java Code:
    for (int i = 0; i < names.length; i++)
    {
      System.out.println(names[i]);
    }

  3. #3
    Sidmyre is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Does this Employee class have a toString() method? If so, you might be able to display it quite simply:

    Java Code:
    for (int i = 0; i < names.length; i++)
    {
      System.out.println(names[i]);
    }

    yea

    Java Code:
    public String toString()
    	{
    		return 	firstName + " " + lastName + " (" + id + ")";
    	}
    although, when I do

    Java Code:
    for (int i = 0; i < names.length; i++)
    {
      System.out.println(names[i]);
    }
    I get " Exception in thread "main" java.lang.NullPointerException"

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    then your code has a bug and the names object is null here. you have to fix this.

    In your code above, I don't see you creating any "new" Employee objects, just a new array. Please know that when you create an array of objects (as opposed to an array of primitive such as ints), you have to create a new object for each item in the array.

  5. #5
    Sidmyre is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default

    ahhh I see what you did there ;P

    got it

    Java Code:
      Employee[] names = new Employee[5];
      names[0] = new Employee("John", "doe", "1234");
      names[1] = new Employee("Mary","berry", "2345");
      names[2] = new Employee("Chris", "bliss", "3456");
      names[3] = new Employee("Angela", "Jolie", "4567");
      names[4] = new Employee("Marky", "mark", "5678");
    gracias
    Last edited by Sidmyre; 12-12-2008 at 02:18 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Good job. So hopefully now it works. Oh, and my girlfriend's name is spelled "Angelina", not "Angela".

Similar Threads

  1. Two Objects
    By losintikfos in forum New To Java
    Replies: 3
    Last Post: 11-14-2008, 07:04 PM
  2. 1 to 1 Objects
    By this.that in forum New To Java
    Replies: 4
    Last Post: 08-07-2008, 10:09 PM
  3. how many objects ?
    By kevinsong in forum Advanced Java
    Replies: 16
    Last Post: 07-16-2008, 05:59 PM
  4. Getting objects from a list
    By markyoung1984 in forum New To Java
    Replies: 4
    Last Post: 03-13-2008, 10:45 PM
  5. Help with Objects!
    By Shorinhio in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •