Results 1 to 15 of 15
  1. #1
    nignogs's Avatar
    nignogs is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Question 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:

    Java 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");
            }
        }
    2nd Method in same Class
    Java 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;
        }
    the First method checks if able to add item to inventory, i keep running into errors in that method
    Java Code:
    String theItem = currentRoom.getRoomItem(foundItem);
    "getRoomItem(java.lang.String) in Room cannot be applied to (boolean)"

    Now this maybe the case as the method below is from another class and I have totally confused myself with variable control types???
    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;
        }
    Do i some how need to convert Boolean to String or am I going about it the wrong way?

    Thanks in advance.

    Kind regards
    NN

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,192
    Rep Power
    19

    Default

    You didn't post the getRoomItem mthod.

    db

  3. #3
    nignogs's Avatar
    nignogs is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    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:

    Java Code:
    /**
         * returns item located in room
         */
        public String getRoomItem(String itemName)
        {
            
            return roomItems.get(itemName);
        }
    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.

    I have this other method in the same class:

    Java 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;
        }
    I think as a newbie to Java I am overwhelmed with how many methods and classes and the interwoven pathways of them all

  4. #4
    nignogs's Avatar
    nignogs is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    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 :)

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    Can you post your latest code?

  6. #6
    nignogs's Avatar
    nignogs is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Can you post your latest code?
    Sure thing:
    be warned since last posting I have been tinkering and have found 60 different ways on how not to get it right :)


    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");
            }
        }
    "inventory.add(theItem);"
    Error: cannot find symbol - method add(java.lang.String)

    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;
        }
    Really dont know what I am doing with this one. seemed to polymorph into haze of confusion. tried adding something like:
    Java Code:
    boolean found = true;
    String foundIt = new boolean(found).toString();
    return foundIt;
    this was an attempt to convert boolean to string as a return value when invoked by the first method addInventoryItem() from another class.

    Regards
    NN

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    OK, now I don't know your requirements so some of this is a guess.
    Java Code:
        public String locateItem(String itemName)
    This should be returning an Item.
    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.

  8. #8
    nignogs's Avatar
    nignogs is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    OK, now I don't know your requirements so some of this is a guess.
    Java Code:
        public String locateItem(String itemName)
    This should be returning an Item.
    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)
    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

    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");
            }
        }
    "Item foundItem = currentRoom.locateItem(itemName);"
    Error: incompatible types - found java.lang.String but expected Item .

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    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.

  10. #10
    nignogs's Avatar
    nignogs is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    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.
    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;
        }

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    You're not returning an Item there.
    You are returning a String.
    This:
    Java Code:
    Item item ...
    is your Item.
    Return it (then fix the method signature to show it is returning an Item and not a String).

  12. #12
    nignogs's Avatar
    nignogs is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    You're not returning an Item there.
    You are returning a String.
    This:
    Java Code:
    Item item ...
    is your Item.
    Return it (then fix the method signature to show it is returning an Item and not a String).
    Okay bare with me here,

    Java 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;
        }
    Now returns an item

    but now another issue has arisen with
    Method
    Java Code:
    addInventoryItem(Command command):
    Item foundItem = currentRoom.locateItem(itemName);
    if(foundItem != null)
            {
                if ((currentweight + item.getItemWeight()) < maxWeight)
                {
    "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.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    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)
    Reposting the relevant bit of code from my post earlier...

  14. #14
    nignogs's Avatar
    nignogs is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    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)
    Reposting the relevant bit of code from my post earlier...
    haha I am slow, but thanks, that fixed that segment.

    be back soon once i have worked a bit more on the
    Java Code:
                 currentRoom.removeRoomItem(foundItem);
                    inventory.add(theItem);
                    currentweight += item.getItemWeight();

  15. #15
    nignogs's Avatar
    nignogs is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default

    Just want to say thanks Toll,

    With your guidance it has helped me overcome that method and i also reworked another method in another class.


    Thanks again mate.

    regards

    NN

Similar Threads

  1. Invalid Method Declaration; Return Type Required
    By bremzb in forum AWT / Swing
    Replies: 3
    Last Post: 05-05-2011, 10:12 PM
  2. Generic method invocation Clarification required
    By muzu1232005 in forum Advanced Java
    Replies: 7
    Last Post: 04-24-2011, 10:59 PM
  3. Err: invalid method declaration; return type required
    By Die The Villain in forum New To Java
    Replies: 12
    Last Post: 04-15-2011, 12:44 AM
  4. NullPointer adding an item to a LinkedList
    By sehudson in forum New To Java
    Replies: 7
    Last Post: 03-10-2011, 03:39 AM
  5. Returning complex data types from a web method
    By Tshegofatsom in forum Advanced Java
    Replies: 6
    Last Post: 05-15-2009, 03:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •