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 returning more then one Item Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "returning more then one Item" Watch "returning more then one Item" New topic
Author

returning more then one Item

Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
I'm adding items to a room in a game and i put more then 1 item in some rooms. In this method it will display the items on the screen:

public String getItemDescription()
{
if(name == null)
return "There are no items in this room";
else
return "There is a " + name + " that weighs " + weight + " pounds.\nIt is a " + Idescription;
}

Right now it prints the last item in the room... how can I get it to display all the items in the room... basically saying that entire return statement for as many items there are in the room?


Live And Let Learn.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Well, presumably if you have more than one item in a room, then you have an array or Collection of items for each room, right? Then your return statement can either return an array or Collection of descriptions, or it can return one long String describing all the items. In either case, you'd need a loop over the array or Collection of items to create the return value.


[Jess in Action][AskingGoodQuestions]
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
You presumed right... I do have a collection of Items and Rooms. But I don't know how to write the loop to do it... I can't seem to wrap my head around how to do it in this case... just a brain fart... buts its hurting!! Any suggestions to get me started on it?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Originally posted by Martin vanPutten:
You presumed right... I do have a collection of Items and Rooms. But I don't know how to write the loop to do it... I can't seem to wrap my head around how to do it in this case... just a brain fart... buts its hurting!! Any suggestions to get me started on it?


How about something like (in pseudo code)...



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

I'm imagining something like, for example,



But obviously the particulars depend a lot on what you actually have. Also if I were writing from scratch, rather than copying your code, I'd use more append() calls and no "+" for String concatenation.
Martin vanPutten
Ranch Hand

Joined: Mar 26, 2006
Posts: 124
Thanks man... that really helped... also I learnt what append does.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: returning more then one Item
 
Similar Threads
Beginner Trying
Getting a single name from an arraylist...
Need help to convert from Con List to Link List
Locked door help
Recursive program help