File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

returning more then one Item

 
Martin vanPutten
Ranch Hand
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martin vanPutten
Ranch Hand
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks man... that really helped... also I learnt what append does.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic