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

Printing Object form an ArrayList

 
Iain Linton
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a new user and a Computer Science Degree 1st year student, I am having some difficulty with retriving an object from an ArrayList, here is my code.

I keep getting "cannot resolve method .print

Any help will be great.

public void printWaitingList()
{
for (Iterator iter = waitingList.iterator() ;
iter.hasNext() ; )
{
Job ajob = (Job)iter.next() ;
waitingList.print() ;
System.out.println() ; // puts line between the jobs
}
}

[ January 24, 2006: Message edited by: Iain Linton ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

From this snippet of code, I can't see what type of object "waitingList" is. Can you tell us?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you mean to do "aJob.print()" instead of "waitingList.print()"? As EFH says, we need to know the type of the waitingList variable. Also, my suggestion might not work since I don't know if the Job class has a print() method.

Layne
 
Iain Linton
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you


[ January 24, 2006: Message edited by: Jim Yingst ]
[ January 29, 2006: Message edited by: Iain Linton ]
 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Layne Lund:
Perhaps you mean to do "aJob.print()" instead of "waitingList.print()"?
I would agree with this. It doesn't make sense to iterate through a list of Job objects and then not do anything with them.
 
Iain Linton
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
I would agree with this. It doesn't make sense to iterate through a list of Job objects and then not do anything with them.


The job has to be passed to the waitingList which is an ArrayList, so the ArrayList now has job objects in it, what needs to happen is when the print method is called the job objects are retrived from the ArrayList and all are printed to the terminal window.

By the way I am using Blue J by David J Barns and Michael Kolling.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the "aJob.print()" suggestion is certainly the right one, or at least on the right track.

Let's go talk about this over in "Java in General (Beginner)."
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want the toString() method in Job to return somthing useful, you are going to have to override the toString() method in Object class and define what "somthing useful" is.
For instance:
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Garrett Rowe:
If you want the toString() method in Job to return somthing useful, you are going to have to override the toString() method in Object class and define what "somthing useful" is.
For instance:


Umm...that's the code for the Student class...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iain - Please use code tags in the future. I've added them you your earlier long code posts. Thanks.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Layne Lund:

Umm...that's the code for the Student class...[/QB]


I know... I was just attempting to show how a meaningful toString() method might look. I was gonna let the OP figure out the toString() method for their class themselves.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Garrett Rowe:


I know... I was just attempting to show how a meaningful toString() method might look. I was gonna let the OP figure out the toString() method for their class themselves.


Doh! I missed the "for instance" just above that code. Sorry about that.

The Job class already has a method that does this. It just isn't called toString().

Layne
[ January 24, 2006: Message edited by: Layne Lund ]
 
Iain Linton
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Layne Lund:


Thank you.

now I just have to get the date added to the output, I have been told to use numCopies * pages * 2 (if DoubleSided).


the question is how do I use the above within the printReport method.

Doh! I missed the "for instance" just above that code. Sorry about that.

The Job class already has a method that does this. It just isn't called toString().

Layne

[ January 24, 2006: Message edited by: Layne Lund ]

[ January 29, 2006: Message edited by: Iain Linton ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are quoting someone, be sure that there is a [ /QUOTE ] tag before your own message. Otherwise it looks like part of the quote.

I hope we have answered your questions about printing out the date in your other thread that you started.

Layne
[ January 26, 2006: Message edited by: Layne Lund ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic