I am trying to implement an item class to add items to rooms in a game in BlueJ. However in the Room class the addItem method is not working because of a problem with the "items.add". I have no idea what I am doing wrong.

public class Room 
{
    private String description;
    private HashMap<String, Room> exits;        // stores exits of this room.
    private ArrayList<Item> items;
    /**
     * Create a room described "description". Initially, it has
     * no exits. "description" is something like "a kitchen" or
     * "an open court yard".
     * @param description The room's description.
     */
    public Room(String description) 
    {
        this.description = description;
        exits = new HashMap<String, Room>();
        ArrayList<Item> items = new ArrayList<Item>();
    }

    /**
     * Define an exit from this room.
     * @param direction The direction of the exit.
     * @param neighbor  The room to which the exit leads.
     */
    public void setExit(String direction, Room neighbor) 
    {
        exits.put(direction, neighbor);
    }

    /**
     * @return The short description of the room
     * (the one that was defined in the constructor).
     */
    public String getShortDescription()
    {
        return description;
    }

    /**
     * Return a description of the room in the form:
     *     You are in the kitchen.
     *     Exits: north west
     * @return A long description of this room
     */
    public String getLongDescription()
    {
        return "You " + description + ".\n" + getExitString() + ".\n" + "The item(s) in this room are: " + getItemString();
    }

    /**
     * Add item when room is created
     */
    public void addItem(String description, int weight)
    {
        items.add(description, weight);
    }

    /**
     * Return a string describing the room's exits, for example
     * "Exits: north west".
     * @return Details of the room's exits.
     */
    private String getExitString()
    {
        String returnString = "Exits:";
        Set<String> keys = exits.keySet();
        for(String exit : keys) {
            returnString += " " + exit;
        }
        return returnString;
    }

    /**
     * Return the room that is reached if we go from this room in direction
     * "direction". If there is no room in that direction, return null.
     * @param direction The exit's direction.
     * @return The room in the given direction.
     */
    public Room getExit(String direction) 
    {
        return exits.get(direction);
    }

    /**
     * 
     */
    public String getItemString()
    {
        String returnString = "";
        {
            returnString += " " + items;
        }
        return returnString;
    }
}

You haven't done anything to implement an Items class. That would be some code that begins

class Item {
   // variables for description and weight
   // contructor
   // etc
}

Then your addItem method should accept an Item as its parameter, so you can add that to the ArrayList.

I already have an Item class

import java.util.Set;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;

/**
 * Used to create and store items
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Item
{
    // instance variables - replace the example below with your own
    private String description;
    public int weight;
    private ArrayList<Item> list;

    /**
     * Constructor for objects of class Item
     */
    public Item(String description, int weight)
    {
        this.description = description;
        weight = weight;
    }

    /**
     * Get description of item
     */
    public String getShortDescription()
    {
        return description;
    }

    /**
     * Get weight of item
     */
    public int weight()
    {
        return weight;
    }

    /**
     * Show description and weight of item
     */
    public String toString()
    {
        return "Item" + description + "Weight" + weight;
    }

    /**
     * 
     */
    public void addItem(String Description,int weight)
    {
        list.add(new Item (description, weight));
    }
    /**
     * Create items
     */
    public void add(Item item)
    {
        list.add(item);
    }

    /**
     * Get item
     */
    public ArrayList<Item> getItems()
    {
        return list;
    }

    /**
     * 
     */
    public void addItem()
    {
        Item item1 = new Item ("energy", 5);
    }
}

Sorry, my remote mind reader is offline today, otherwize I would have immediately known about that. :)

In that case just straight to the last line of my previous post

Would this be correct in the Room class?

/**
     * Add item when room is created
     */
    public void addItem(Item item)
    {
        items.add(description, weight);
    }

Its better...
inside the method you need to add that Item to the items list

Thanks for the help. However it will not compile because it cant find "weight" and doesnt "items.add" add the item to the items list?

items.add will only add an object of the Item class because that's how the list is defined. There is not, and has never been, and add method for lists that takes two parameters String and int - check the API documentation.
Of course it can't find "weight" - you have no declaration for that variable now.
Please take a little more time to think about your code before posting "what's wrong with my code" threads.

Thank you and sorry I was rushing to figure it out and neglected to do what I should have.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.