File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Printing Object form an ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing Object form an ArrayList" Watch "Printing Object form an ArrayList" New topic
Author

Printing Object form an ArrayList

Iain Linton
Greenhorn

Joined: Jan 24, 2006
Posts: 21
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

From this snippet of code, I can't see what type of object "waitingList" is. Can you tell us?


[Jess in Action][AskingGoodQuestions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Iain Linton
Greenhorn

Joined: Jan 24, 2006
Posts: 21
Thank you


[ January 24, 2006: Message edited by: Jim Yingst ]
[ January 29, 2006: Message edited by: Iain Linton ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Jan 24, 2006
Posts: 21
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Jan 17, 2006
Posts: 1296
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:


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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

Joined: Jan 30, 2000
Posts: 18671
Iain - Please use code tags in the future. I've added them you your earlier long code posts. Thanks.


"I'm not back." - Bill Harding, Twister
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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

Joined: Dec 06, 2001
Posts: 3061
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

Joined: Jan 24, 2006
Posts: 21
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

Joined: Dec 06, 2001
Posts: 3061
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing Object form an ArrayList
 
Similar Threads
Copy an ArrayList
Filtering an ArrayList
This question is about AutoBoxing feature in java
mock test for generics & collections
cant print Date or time