Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Append objects to List and show them.

 
Ronald Spina
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,

i'm new to java and currently doing some practice.....
The below code consists of a MainProgram, an class Employee which return an object of Employee, created a list, and nodes.
My problem is that i am not sure if the object are being stored into the list. I've used the method append() to add to list and
the method listAll() to show the object stored into the list.

I want to print them on the screen when user chooses case 2.

I would appreciate for suggestion.. or where is my mistake because program is compiling ok but cannot output on the screen my list!!

Thanks

 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a lot of (unformatted, not code-tagged) code to test whether you can add items to a list and then print them out. For your own sanity's sake and to make helping you easier, you should break your projects into smaller bits, each with specific functional goals that you can test and then add to a larger project later when you've verified they do what they're supposed to do.

Edit: Helpers added code tags. Thanks.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12180
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added 'code tags' to your post. Notice how the formatting is preserved, making it much easier to read. Next time, after you paste in your code, highlight it and click the 'code' button above, just like you wanted to make it italic or bold.

I don't have time to look at your code in detail, but I can say that System.out.println() can be your best friend when trying to figure out what your code is doing. For example, you could print the size of your collection after each append() call to make sure it is growing appropriately.

If i have more time later (and nobody beats me to it), I'll try to take a look.
 
Brian Burress
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
myList is declared in main (line 17) and again in createEmployees (line 95). If you reference myList in createEmployees (such as append on line 101), it will be local to createEmployees and gone when you exit the method.



 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your main method, you declare and initialise myList but don't populate it anywhere.
 
Ronald Spina
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry guys for the lot of unformatted code but this was my first time posting in here...
So where should I reference my list?

And does this mean that I should write these codes in case 2:
System.outprintln.out(myList.listAll());
 
Ronald Spina
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And another thing guys does for e.g.

myList.add()
myList.append() means the same thing?
I'm using myList.append(e); in my case where Employee = e... (object)
 
Pavan Kumar Dittakavi
Ranch Hand
Posts: 106
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At line 17, I could not stop noticing but, the following statement exists:

. List is an interface. And we cannot create an object out of it. How did the compile?. Am I missing anything here?

And by the API specs, add method appends to the list. So, they might mean the same thing.

Thanks,
Pavan.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pavan Kumar Dittakavi wrote:At line 17, I could not stop noticing but, the following statement exists:

. List is an interface. And we cannot create an object out of it. How did the compile?. Am I missing anything here?

If you look at line 173 and later, you see that Ronald has defined his own class called List. So it's not java.util.List being used here, but Ronald's own class List.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ronald Spina wrote:And another thing guys does for e.g.

myList.add()
myList.append() means the same thing?
I'm using myList.append(e); in my case where Employee = e... (object)

Your class List doesn't have an add() method, so no, add() and append() don't mean the same thing.

People are getting confused because you've defined your own class List, you're not using the standard List interface from the package java.util. Usually it is not a good idea to name classes with the same name as some standard interface or class, because it creates confusion.
 
Pavan Kumar Dittakavi
Ranch Hand
Posts: 106
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Jesper..I guess I missed that. But one more question. In the first class, he imported the util package entirely, and then he is creating the List object. Why isn't the compiler generating an error? Why is it mapping to the one he defined and not the one in the util package?

Thanks,
Pavan.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got such a large main method? The ideal length for a main method is one statement? Why have you got so many static members in that first class?
The javac tool starts looking for classes in the current folder; if a class is found matching the name in the class, it presumes that is the class wanted. To demonstrate that phenomenon, try creating a class called String and see how many errors you can count!
 
Ronald Spina
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for so many replies.....
So first thing i should do is to change to name of the class List right.. maybe EmployeeList should be fine.

Then I need to System.out.print(myList.listAll()); to see if object are being stored.

I''ll try when I go home and hopefully will work.
Any further suggestion please forward.
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic