Google Answers Logo
View Question
 
Q: Java - cannot resolve symbol ( No Answer,   3 Comments )
Question  
Subject: Java - cannot resolve symbol
Category: Computers > Programming
Asked by: drunkpanda-ga
List Price: $10.00
Posted: 01 Apr 2005 01:52 PST
Expires: 01 May 2005 02:52 PDT
Question ID: 503517
i thought it would be easier that i post all the code below and you run it 
you will see the problem like this: 
"Cannot resolve symbol - consturctor Room (java.jang.String)"
i guess it would be reasonably easy to fix but as i am a super
beginner, i tired but i wasnot be able to fix the problem
can sometbody please just fix the problem for me 
thank you very much   P.S. the development tool is BlueJ
------------------------------------------------------------
Classes: Game, Parser, CommandWords, Command, Room, Item, Player
------------------------------------------------------------
public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player currentPlayer;
   
   
        
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        
        currentPlayer = new Player("Mark",currentRoom);
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room outside, theatre, pub, lab, office, cellar;
      
        // create the rooms
        outside = new Room("outside the main entrance of the Polytechnic");
        theatre = new Room("in a lecture theatre");
        pub = new Room("in the campus pub");
        lab = new Room("in a computing lab");
        office = new Room("in the computing admin office");
        cellar = new Room("in the cellar");
        // initialise room exits
        outside.setExit("east", theatre);
        outside.setExit("south", lab);
        outside.setExit("west", pub);

        theatre.setExit("west", outside);

        pub.setExit("east", outside);

        lab.setExit("north", outside);
        lab.setExit("east", office);
        

        office.setExit("west", lab);
        office.setExit("down", cellar);
        cellar.setExit("up", office);
        currentRoom = outside;  // start game outside
        
        // add items 
        Item textbook = new Item("this is a Java textbook",5);
        office.addItem("Textbook",textbook);
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void start() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("Thank you for playing.  Good bye.");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("Welcome to the World of Zuul!");
        System.out.println("World of Zuul is a new, incredibly boring
adventure game.");
        System.out.println("Type 'help' if you need help.");
        System.out.println();
        System.out.println(currentRoom.getLongDescription());
    }

    /**
     * Given a command, process (that is: execute) the command.
     * If this command ends the game, true is returned, otherwise false is
     * returned.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("I don't know what you mean...");
            return false;
        }

        String commandWord = command.getCommandWord();
        if (commandWord.equals("help"))
            printHelp();
        else if (commandWord.equals("go"))
            goRoom(command);
        else if (commandWord.equals("look"))
            look(command);
        else if (commandWord.equals("take"))
            take(command);
        else if (commandWord.equals("drop"))
            drop(command);
        else if (commandWord.equals("quit")){
            wantToQuit = quit(command);
        }
        return wantToQuit;
    }

    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("You are lost. You are alone. You wander");
        System.out.println("around at the university.");
        System.out.println();
        System.out.println("Your command words are:");
        parser.showCommands();
    }

    /** 
     * Try to go to one direction. If there is an exit, enter the new
     * room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("Go where?");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.getExit(direction);

        if (nextRoom == null)
            System.out.println("There is no door!");
        else {
            currentRoom = nextRoom;
            System.out.println(currentRoom.getLongDescription());
        }
    }

    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game. Return true, if this command
     * quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("Quit what?");
            return false;
        }
        else
            return true;  // signal that we want to quit
    }
    /**
     * "Look" was entered. look around if there is any item in a room 
     */
    private void look(Command command)
    {

        System.out.println(currentRoom.getMyItemsDescription());
    }
    
    /**
     * "Take" was entered. take a item in a room, if there is any
     */
    private void take(Command command)
    {
      String theItem = command.getSecondWord();
        Item theItems = currentRoom.getItem(theItem);
        
            if (!command.hasSecondWord()){
                System.out.println("Take what?");
            return;
            }
            if(currentPlayer.getMyCarriedWeight() +
theItems.getWeight() > currentPlayer.getMaxWeight()){
                System.out.println("This Item is too heavy for you to carry!");
            return;
            }
            if(theItems.isMoveable()){ 
                System.out.println("You can't move the item");
            return;
            }
            
            currentPlayer.addItem(theItems);
            System.out.println("The item has been taken");
            currentRoom.removeItem(theItems);
    }
    
     private void drop(Command command)
   {
        String theItem = command.getSecondWord();
        Item theItems = currentPlayer.getItem(theItem);
        
        if (!command.hasSecondWord()){
                System.out.println("Drop what?");
            return;
            }
        
        currentPlayer.removeItem(theItems);
        System.out.println("you have dropped the item");
        currentRoom.addItem(theItems);
   }
    /**
     * "ask" was entered. to get more information
     */
    private void ask()
    {
        System.out.println("No one hear you");
    }
}
------------------------------------------------------------
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Parser   
{

    private CommandWords commands;  // holds all valid command words

    public Parser() 
    {
        commands = new CommandWords();
    }

    public Command getCommand() 
    {
        String inputLine = "";   // will hold the full input line
        String word1;
        String word2;

        System.out.print("> ");     // print prompt

        BufferedReader reader = 
            new BufferedReader(new InputStreamReader(System.in));
        try {
            inputLine = reader.readLine().trim().toLowerCase();
        }
        catch(java.io.IOException exc) {
            System.out.println ("There was an error during reading: "
                                + exc.getMessage());
        }

        StringTokenizer tokenizer = new StringTokenizer(inputLine);

        if(tokenizer.hasMoreTokens())
            word1 = tokenizer.nextToken();      // get first word
        else
            word1 = null;
        if(tokenizer.hasMoreTokens())
            word2 = tokenizer.nextToken();      // get second word
        else
            word2 = null;

        // note: we just ignore the rest of the input line.

        // Now check whether this word is known. If so, create a command
        // with it. If not, create a "null" command (for unknown command).

        if(commands.isCommand(word1))
            return new Command(word1, word2);
        else
            return new Command(null, word2);
    }

    /**
     * Print out a list of valid command words.
     */
    public void showCommands()
    {
        commands.showAll();
    }
}
----------------------------------------------------------------
public class CommandWords
{
    // a constant array that holds all valid command words
    private static final String[] validCommands = {
        "go", "quit", "help", "look", "take", "drop"
    };

    /**
     * Constructor - initialise the command words.
     */
    public CommandWords()
    {
        // nothing to do at the moment...
    }

    /**
     * Check whether a given String is a valid command word. 
     * Return true if it is, false if it isn't.
     */
    public boolean isCommand(String aString)
    {
        for(int i = 0; i < validCommands.length; i++) {
            if(validCommands[i].equals(aString))
                return true;
        }
        // if we get here, the string was not found in the commands
        return false;
    }

    /*
     * Print all valid commands to System.out.
     */
    public void showAll() 
    {
        for(int i = 0; i < validCommands.length; i++) {
            System.out.print(validCommands[i] + "  ");
        }
        System.out.println();
    }
}
--------------------------------------------------------------
public class Command
{
    private String commandWord;
    private String secondWord;

    /**
     * Create a command object. First and second word must be supplied, but
     * either one (or both) can be null. The command word should be null to
     * indicate that this was a command that is not recognised by this game.
     */
    public Command(String firstWord, String secondWord)
    {
        commandWord = firstWord;
        this.secondWord = secondWord;
    }

    /**
     * Return the command word (the first word) of this command. If the
     * command was not understood, the result is null.
     */
    public String getCommandWord()
    {
        return commandWord;
    }

    /**
     * Return the second word of this command. Returns null if there was no
     * second word.
     */
    public String getSecondWord()
    {
        return secondWord;
    }

    /**
     * Return true if this command was not understood.
     */
    public boolean isUnknown()
    {
        return (commandWord == null);
    }

    /**
     * Return true if the command has a second word.
     */
    public boolean hasSecondWord()
    {
        return (secondWord != null);
    }
}

-------------------------------------------------------
import java.util.Set;
import java.util.HashMap;
import java.util.Iterator;
import java.util.HashSet;
public class Room 
{
    private String description;
    private HashMap exits;        // stores exits of this room.
    private String name;
    private HashSet myItems;
 

    /**
     * Create a room described "description". Initially, it has no exits.
     * "description" is something like "in a kitchen" or "in an open court 
     * yard".
     */
    public Room(String description, String name) 
    {
        this.description = description;
        this.name = name;
        exits = new HashMap();
        myItems = new HashSet();
       
    }

    /**
     * Define an exit from this room.
     */
    public void setExit(String direction, Room neighbor) 
    {
        exits.put(direction, neighbor);
    }
    

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

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

    /**
     * Return a string describing the room's exits, for example
     * "Exits: north west".
     */
    private String getExitString()
    {
        String returnString = "Exits:";
        Set keys = exits.keySet();
        for(Iterator iter = keys.iterator(); iter.hasNext(); )
            returnString += " " + iter.next();
        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.
     */
    public Room getExit(String direction) 
    {
        return (Room)exits.get(direction);
    }
    
    /**
     * Add items to the rooms
     */
    public void addItem(Item myItem)
    {
        myItems.add(myItem);
    }
    public void removeItem(Item myItem)
    {
        myItems.remove(myItem);
    }
    public Item getItem(String theItem)
    {
        Iterator iter = myItems.iterator();
        while(iter.hasNext())
        {
            Item anItem = (Item) iter.next();
            if(anItem.getName().equals(theItem))
            return anItem;
        }
        return null;
    }
    public String getMyItemsDescription()
    {
        String returnString = "This room has items:";
        for(Iterator iter = myItems.iterator(); iter.hasNext();)
            returnString += " "+ ((Item)iter.next()).getDescription();
        return returnString;
    }
    public String getName()
    {
        return name;
    }
}

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


public class Item
{
    private String description;
    private String name;
    private int weight;
    private boolean moveable;


    /**
     * Constructor for objects of class Item.
     */
    public Item(String myDescription, String myName, int myWeight,
boolean itemMoveable)
    {
        this.description = myDescription;
        this.name = myName;
        this.weight = myWeight;
        this.moveable = itemMoveable;       
    }

    /**
     * Return the description of the item (the one that was defined
     * in the constructor).
     */
    public String getDescription()
    {
        return description;
    }
    
    /**
     * Return the name of the item (the one that was defined
     * in the constructor).
     */
    public String getName()
    {
        return name;
    }
    
    public boolean isMoveable()
    {
        return (moveable == false);
    }
    
    /**
     * Return the weight of the item 
     */
    public int getWeight()
    {
        return weight;
    }
    
}
-------------------------------------------------------------------------
import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;

public class Player
{
    private String name;
    //private int weightCarried;
    private static int maxWeight = 300;
    private Room currentRoom;
    private HashSet myItems;


    /**
     * Constructor for objects of class Player.
     */
    public Player(String myName, Room myCurrentRoom) 
    {
        name = myName;
        currentRoom = myCurrentRoom;
        myItems = new HashSet();
    }

    /**
     * Return the name of the player (the one that was defined
     * in the constructor).
     */
    public String getName()
    {
        return name;
    }
    
    public HashSet getMyItems()
    {
        return myItems;
    }
    
    /**
     * Return the amount of weight the player is carrying.
     */
    public int getMyCarriedWeight()
    {
        Iterator iter = myItems.iterator();
        int total = 0;
        while(iter.hasNext()){
            total += ((Item) iter.next()).getWeight();
        }
        return total;
    }
    
    public static int getMaxWeight()
    {
        return maxWeight;
    }
    
    /*
     * Add an item to the player's collection of items. Items have a
description, name and weight.
     */ 
    public void addItem(Item myItem)
    {
        myItems.add(myItem);
    }
    
     public Item getItem(String theItem)
    {
        Iterator iter = myItems.iterator();
        while(iter.hasNext()){
           Item anItem = (Item) iter.next();
                if(anItem.getName().equals(theItem))
                    return anItem;                  
           }
           return null;
    }
    
     public String getMyItemsDescription()
    {
      String returnString = "The items in my collection are: "; 
      for(Iterator iter = myItems.iterator(); iter.hasNext();)
        returnString += " " + ((Item)iter.next()).getDescription();
      return returnString;
    }
    
    
    public void removeItem(Item myItem)
    {
        myItems.remove(myItem);
    }
        
}

Clarification of Question by drunkpanda-ga on 01 Apr 2005 15:26 PST
hi O once again thank you so much you have answered my question

the problems seem so easy to fix with your experiences and knowladge
to be honest with you the problems actually took me several hours to
find how to fix them and i still couldn't.
the ideas of Object Oriented are very difficult for me to understand
especially when i encounter using classes.

thank you for your help and P.S. if i have problem again how can i ask
for you help

Drunkpanda

Clarification of Question by drunkpanda-ga on 01 Apr 2005 15:36 PST
Hi O

I am still trying to improve the game and i think i will encounter
more problems as expected so can we stay in this thread for a while i
think i will ask you more questions soon

please tell me if thats ok and thank you 

drunkpanda
Answer  
There is no answer at this time.

Comments  
Subject: Re: Java - cannot resolve symbol
From: on8-ga on 01 Apr 2005 02:21 PST
 
Hello,

Your code:
 private void createRooms()
    {
        Room outside, theatre, pub, lab, office, cellar;
      
        // create the rooms
-->      outside = new Room("outside the main entrance of the Polytechnic");

At this point you are trying to construct a new instance of Room by
passing in a single String parameter which is the description.

--> public Room(String description, String name) 

But your Room constructor takes two parameters, description and name. 
To fix this particular compilation error, pass in two paramaters, for
example:-

--> outside = new Room("outside the main entrance of the Polytechnic", "outside");

Regards

O
Subject: Re: Java - cannot resolve symbol
From: drunkpanda-ga on 01 Apr 2005 04:20 PST
 
hi O thank you for replying my question

i have done what you said and the problem solved
 outside = new Room("outside the main entrance of the Polytechnic","outside");

and changed this line below,
 Item textbook = new Item("this is a Java textbook","textbook",30,true);

but there is a problem with this line with error "addItem(Item) in
Room cannot be applied to (java.lang.String,Item)"
office.addItem("Textbook",textbook);

P.S. if farther answer need raise the price please let me know

DrunkPanda
Subject: Re: Java - cannot resolve symbol
From: on8-ga on 01 Apr 2005 07:16 PST
 
Hello DrunkPanda,

--> Item textbook = new Item("this is a Java textbook","textbook",30,true);

You have correctly changed this line, as the Item Constructor takes
the four parameters.  But this is unrelated to your next problem.

--> office.addItem("Textbook",textbook);

In order to fix this problem, follow the below logic:-
What is Office -> this is a Room.
What is addItem -> this is a method called on the Room instance.
Therefore, look in the Room Class, more specifically the addItem method.

--> public void addItem(Item myItem)

This method takes only one parameter; an Item object.  But the failing
line is attempting to addItem with two parameters
(office.addItem("Textbook",textbook);
) namely a String and an Item textbook.

In order to fix this, it is only required to pass in the single Item parameter.

--> office.addItem(textbook);

Regards

O

These comments are free.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy