Results 1 to 15 of 15
- 05-11-2011, 09:00 AM #1
Help required with complex method: adding item
Hi Guys,
I have been stuck for many hours and have slowly progressed one error after another, well I think i have progressed :) lol.
the two methods below belong to the same class:
2nd Method in same ClassJava Code:/** * Add item to inventory * */ public void addInventoryItem(Command command) { String itemName = command.getSecondWord(); boolean foundItem = currentRoom.locateItem(itemName); if(foundItem == true) { String theItem = currentRoom.getRoomItem(foundItem); Item item = (Item) theItem; if ((currentweight + item.getItemWeight()) < maxWeight) { currentRoom.removeRoomItem(foundItem); inventory.add(theItem); currentweight += item.getItemWeight(); } else { System.out.print("Unable to get item you are carrying to much"); } } else { System.out.print("the item: " + itemName + " does not exist"); } }
the First method checks if able to add item to inventory, i keep running into errors in that methodJava Code:/** * Locates and Returns if item exists in player Inventory */ public boolean locateItem(String itemName) { int index; Iterator it = inventory.iterator(); for(index = 0; it.hasNext(); index++) { Item item = (Item) it.next(); if(item.getItemName().equals(itemName)) { return true; } } return false; }
"getRoomItem(java.lang.String) in Room cannot be applied to (boolean)"Java Code:String theItem = currentRoom.getRoomItem(foundItem);
Now this maybe the case as the method below is from another class and I have totally confused myself with variable control types???
Do i some how need to convert Boolean to String or am I going about it the wrong way?Java Code:/** * Locates and Returns if item exists in player Inventory */ public boolean locateItem(String itemName) { int index; Iterator it = roomItems.iterator(); for(index = 0; it.hasNext(); index++) { Item item = (Item) it.next(); if(item.getItemName().equals(itemName)) { return true; } } return false; }
Thanks in advance.
Kind regards
NN
- 05-11-2011, 09:08 AM #2
You didn't post the getRoomItem mthod.
db
- 05-11-2011, 09:17 AM #3
Hi,
Ooops, and on that note i think thats where "one" of my problems lies.
Method so far to get room item is as follows:
Now i don't know why i had that, as .get() only returns index id (int) of element in the arraylist not a String :( fail by me.Java Code:/** * returns item located in room */ public String getRoomItem(String itemName) { return roomItems.get(itemName); }
I have this other method in the same class:
I think as a newbie to Java I am overwhelmed with how many methods and classes and the interwoven pathways of them allJava Code:/** * Locates and Returns if item exists in Room */ public boolean locateItem(String itemName) { int index; Iterator it = roomItems.iterator(); for(index = 0; it.hasNext(); index++) { Item item = (Item) it.next(); if(item.getItemName().equals(itemName)) { return true; } } return false; }
- 05-11-2011, 09:24 AM #4
Hi again,
After removing broken method getRoomItem()
I made change to addInventoryItem(Command command)
issue now is defining the variable
String theItem = currentRoom.locateItem(foundItem);
generates error in compiler:
"locateItem(java.lang.String) in Room cannot be applied to (boolean)"
one step fwd one step back :)
- 05-11-2011, 10:39 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Can you post your latest code?
- 05-11-2011, 10:48 AM #6
Sure thing:
be warned since last posting I have been tinkering and have found 60 different ways on how not to get it right :)
"inventory.add(theItem);"Java Code:/** * Add item to inventory * */ public void addInventoryItem(Command command) { String itemName = command.getSecondWord(); String foundItem = currentRoom.locateItem(itemName); if(foundItem == itemName) { String theItem = currentRoom.locateItem(foundItem); Item item; if ((currentweight + item.getItemWeight()) < maxWeight) { currentRoom.removeRoomItem(foundItem); inventory.add(theItem); currentweight += item.getItemWeight(); } else { System.out.print("Unable to get item, as you are carrying to much"); } } else { System.out.print("the item: " + itemName + " does not exist"); } }
Error: cannot find symbol - method add(java.lang.String)
Really dont know what I am doing with this one. seemed to polymorph into haze of confusion. tried adding something like:Java Code:/** * Locates and Returns if item exists in Room */ public String locateItem(String itemName) { final String notFound = "notFound"; int index; Iterator it = roomItems.iterator(); for(index = 0; it.hasNext(); index++) { Item item = (Item) it.next(); if(item.getItemName().equals(itemName)) { return itemName; } } return notFound; }
this was an attempt to convert boolean to string as a return value when invoked by the first method addInventoryItem() from another class.Java Code:boolean found = true; String foundIt = new boolean(found).toString(); return foundIt;
Regards
NN
- 05-11-2011, 10:59 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
OK, now I don't know your requirements so some of this is a guess.
This should be returning an Item.Java Code:public String locateItem(String itemName)
After all, it goes through the array and finds the Item with the same name, if it exists in that array...however it doesn't return it. And since you need that Item later on it makes sense if you return it here. If it doesn't exist then return null.
This means the other code becomes
Java Code:Item foundItem = currentRoom.locateItem(itemName); if(foundItem != null) { // No longer need the next two lines. // String theItem = currentRoom.locateItem(foundItem); // Item item; // Everything from here that attempted to use item can now use foundItem if ((currentweight + foundItem.getItemWeight()) < maxWeight)Last edited by Tolls; 05-11-2011 at 10:59 AM. Reason: typo in code.
- 05-11-2011, 11:28 AM #8
Thanks Toll for your response,
The code you suggested doesn't seem to work with what i require.
1: String itemName = command.getSecondWord();
- sets itemName to the second command word
e.g. get book {book being the second word)
2: I am trying to firstly find if the item exists in the collection of
which there are two collections one is for the player and one is for the room.
If item is found then check if currentWeight of player and itemWeight is less than maxWeight
If so then
Remove item from Room Collection
add item to player collection
set the currentWeight of player.
I tried implementing what you suggested but it generates errors
"Item foundItem = currentRoom.locateItem(itemName);"Java Code:/** * Add item to inventory * */ public void addInventoryItem(Command command) { String itemName = command.getSecondWord(); Item foundItem = currentRoom.locateItem(itemName); if(foundItem != null) { if ((currentweight + item.getItemWeight()) < maxWeight) { currentRoom.removeRoomItem(foundItem); inventory.add(theItem); currentweight += item.getItemWeight(); } else { System.out.print("Unable to get item, as you are carrying to much"); } } else { System.out.print("the item: " + itemName + " does not exist"); } }
Error: incompatible types - found java.lang.String but expected Item .
- 05-11-2011, 11:31 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
I did say you had to change locateItem, so it returned an Item, not a String. This involves changing the signature and the code inside the method.
- 05-11-2011, 11:37 AM #10
Something like below which is pretty much what i had in my 4th post.
sorry but i am real beginner.
Java Code:/** * Locates and Returns if item exists in Room */ public String locateItem(String itemName) { int index; Iterator it = roomItems.iterator(); for(index = 0; it.hasNext(); index++) { Item item = (Item) it.next(); if(item.getItemName().equals(itemName)) { return itemName; } } return null; }
- 05-11-2011, 11:41 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
You're not returning an Item there.
You are returning a String.
This:
is your Item.Java Code:Item item ...
Return it (then fix the method signature to show it is returning an Item and not a String).
- 05-11-2011, 11:58 AM #12
Okay bare with me here,
Now returns an itemJava Code:/** * Locates and Returns if item exists in Room */ public Item locateItem(String itemName) { int index; Iterator it = roomItems.iterator(); for(index = 0; it.hasNext(); index++) { Item item = (Item) it.next(); if(item.getItemName().equals(itemName)) { return item; } } return null; }
but now another issue has arisen with
Method
"if ((currentweight + item.getItemWeight()) < maxWeight)"Java Code:addInventoryItem(Command command): Item foundItem = currentRoom.locateItem(itemName); if(foundItem != null) { if ((currentweight + item.getItemWeight()) < maxWeight) {
Error: cannot find symbol - variable item.
this is driving me to drink.Last edited by nignogs; 05-11-2011 at 12:02 PM.
- 05-11-2011, 12:08 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Reposting the relevant bit of code from my post earlier...Java Code:Item foundItem = currentRoom.locateItem(itemName); if(foundItem != null) { // No longer need the next two lines. // String theItem = currentRoom.locateItem(foundItem); // Item item; [B]// Everything from here that attempted to use item can now use foundItem [/B] if ((currentweight + [B]foundItem[/B].getItemWeight()) < maxWeight)
- 05-11-2011, 12:19 PM #14
- 05-11-2011, 01:06 PM #15
Similar Threads
-
Invalid Method Declaration; Return Type Required
By bremzb in forum AWT / SwingReplies: 3Last Post: 05-05-2011, 10:12 PM -
Generic method invocation Clarification required
By muzu1232005 in forum Advanced JavaReplies: 7Last Post: 04-24-2011, 10:59 PM -
Err: invalid method declaration; return type required
By Die The Villain in forum New To JavaReplies: 12Last Post: 04-15-2011, 12:44 AM -
NullPointer adding an item to a LinkedList
By sehudson in forum New To JavaReplies: 7Last Post: 03-10-2011, 03:39 AM -
Returning complex data types from a web method
By Tshegofatsom in forum Advanced JavaReplies: 6Last Post: 05-15-2009, 03:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks