Results 1 to 16 of 16
  1. #1
    uhertz is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default Cannot return items from a hashmap

    Room values are still null

    <>code<>
    Here is main:
    import java.util.*;
    public class testx extends junit.framework.TestCase
    {
    private Parser parser1;
    private Game game1;
    private ArrayList<String> cmds;
    private Cmd c1;
    private Room cr;

    public testx(){
    }


    protected void setUp()
    {
    parser1 = new Parser();
    game1 = new Game();
    cmds=new ArrayList<String>();
    cr=new Room("test");

    }


    protected void tearDown()
    {
    }


    public void fA(){
    cmds.add(new String("go east"));
    cmds.add(new String("go west"));
    cmds.add(new String("go north"));
    cmds.add(new String("go south"));
    cmds.add(new String("go pub"));
    cmds.add(new String("go outside"));
    cmds.add(new String("go lab"));
    cmds.add(new String("go office"));
    }

    public void testTd(){
    System.out.print('\u000C');
    fA();

    int i=0; boolean finished=false;
    game1.createRooms();
    while (i<cmds.size()){
    String s=cmds.get(i);
    Cmd dmc=parser1.getCommand(s);
    finished = game1.process(dmc);

    i++;
    }
    }

    }

    public class Game
    {
    private Parser parser;
    private Room currentRoom;
    private Room nextRoom;

    public Game()
    {

    parser=new Parser();
    }


    public void createRooms()
    {
    Room outside, theatre, pub, lab, office;
    outside = new Room("outside the main entrance of the university");
    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");

    outside.setExits(null, theatre, lab, pub);
    theatre.setExits(null, null, null, outside);
    pub.setExits(null, outside, null, null);
    lab.setExits(outside, office, null, null);
    office.setExits(null, null, null, lab);
    currentRoom = outside;
    System.out.println(currentRoom.getExitString());

    }

    public void pl()
    {

    System.out.print("Exits: " + currentRoom.getExitString());
    }
    public boolean process(Cmd 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("quit"))
    wantToQuit = quit(command);
    return wantToQuit;
    }
    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:");
    System.out.println(" go quit help");
    }
    public void goRoom(Cmd command)
    {

    if(!command.hasSecondWord()) {
    System.out.println("Go where?");
    return;
    }
    String direction = command.getSecondWord();
    Room nextRoom=currentRoom.getExit(direction);
    System.out.println(currentRoom.getExit(direction)) ;
    if (nextRoom==null){ System.out.println("There is no door!");
    }
    else{
    System.out.println("Exit: "+ nextRoom);
    }
    }

    private boolean quit(Cmd command)
    {
    if(command.hasSecondWord()) {
    System.out.println("Quit what?");
    return false;
    }
    else {
    return true; // signal that we want to quit
    }
    }
    }


    import java.util.HashMap;
    import java.util.Set;
    public class Room
    {
    private String description;
    private HashMap<String, Room> exits;

    public Room (String description)
    {
    this.description=description;
    exits=new HashMap<String, Room>();
    }
    public void setExits(Room north, Room east, Room south, Room west)
    {
    if (north!=null)
    exits.put("north", north);
    System.out.println(" " + west);
    if(east !=null)
    exits.put("east",east);
    if(south!=null)
    exits.put("south",south);
    if(west!=null)
    exits.put("west",west);

    }

    public Room getExit(String direction)
    {
    return exits.get(direction);

    }

    public String getDescription()
    {
    return description;
    }
    public String getExitString(){
    String returnString="Exits: ";
    Set<String> keys=exits.keySet();
    for (String exit: keys) {
    returnString += " " + exit;
    }
    return returnString;

    }

    }
    public class Parser
    {
    private CommandWords commands; // holds all valid command words
    private Scanner reader; // source of command input

    public Parser()
    {
    commands = new CommandWords();
    reader = new Scanner(System.in);
    }

    public Cmd getCommand(String s)
    {
    String inputLine; // will hold the full input line
    String word1 = null;
    String word2 = null;
    inputLine = s;
    Scanner tokenizer = new Scanner(inputLine);
    if(tokenizer.hasNext()) {
    word1 = tokenizer.next(); // get first word
    if(tokenizer.hasNext()) {
    word2 = tokenizer.next();
    }
    }

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

    }

    public class Cmd
    {
    private String commandWord;
    private String secondWord;

    public Cmd(String firstWord, String secondWord)
    {
    commandWord = firstWord;
    this.secondWord = secondWord;
    }

    public String getCommandWord()
    {
    return commandWord;
    }


    public String getSecondWord()
    {
    return secondWord;
    }

    public boolean isUnknown()
    {
    return (commandWord == null);
    }

    /**
    * @return true if the command has a second word.
    */
    public boolean hasSecondWord()
    {
    return (secondWord != null);
    }
    }
    Room@7a5a19
    Room@b122a1
    null
    null
    Room@1589e56
    Exits: south east west
    Room@4c47db
    Exit: Room@4c47db
    Room@7a5a19
    Exit: Room@7a5a19
    null
    There is no door!
    Room@1589e56
    Exit: Room@1589e56
    null
    There is no door!
    null
    There is no door!
    Last edited by uhertz; 06-17-2011 at 05:32 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    All of the rooms returned are null
    Returned to where? Perhaps you could post the code that generated that output, and say what output you were expecting.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Yes, no main method makes it a challenge. We can't magically know what method calls produced your output. Also, when posting code, use code tags
    [code]
    YOUR CODE HERE
    [/code]

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    System.out.println(" " + west);
    Better would be to add an id so you know which line did the printing:
    System.out.println("west= " + west);

    Java Code:
    exits=new HashMap<String, Room>();
    Looks like in the Room constructor you create a new EMPTY exits object.

  5. #5
    uhertz is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    Hi
    I do fill the exits object. I've added a whole whack of code in the original post

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    You need to add more information to the printouts you are making.
    Besides just printing a message, show the values of the variables that caused that message to be printed.
    When you do that you will see what the problem with your program is.

    Also add toString methods to some of the classes.

  7. #7
    uhertz is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    ok will do. This is a real pain in the ass :-(
    The basic problem is that I can put values into the hashmap but cannot retrieve them.
    the returned value is null

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    This is a real pain in the ass
    Better get used to it. Programming takes a lot of attention to details.

  9. #9
    uhertz is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    No luck at all. Seems that the hashmap is not being populated

  10. #10
    uhertz is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    Can I close this and post to new trhead?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    I don't think you added enough printlns to show the problem. If you put them in the right place the output will show you what the problem is.
    Copy and paste here what you print out.
    For example, add the direction to the println that makes this printout:
    There is no door!

  12. #12
    uhertz is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    "There is no door" is correct. The problem is with the first 3 lines of output:
    I'm only concerned with the first three lines of output. In effect, the values are not being stored in the hashmap for some reason

    The expected result for the first three lines is:

    south theater
    east lab
    west pub

    ACTUAL OUTPUT
    south Room@659db7
    east Room@1556d12
    west Room@16be68f
    Direction: east NextRoom Room@1556d12
    Exit: Room@1556d12
    Direction: west NextRoom Room@16be68f
    Exit: Room@16be68f
    Direction: north NextRoom null
    There is no door!
    Direction: south NextRoom Room@659db7
    Exit: Room@659db7
    Direction: pub NextRoom null
    There is no door!
    Direction: outside NextRoom null
    There is no door!
    Direction: lab NextRoom null
    There is no door!
    Direction: office NextRoom null
    There is no door!

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Add a toString() method to the Room class so you can see the description of the Room vs seeing Room@1556d12
    which is a generated value for a Room object.

    the values are not being stored in the hashmap
    What values?
    Where do you put them in the hashmap?

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    The expected result for the first three lines is:

    south theater
    east lab
    west pub
    What line(s) of your code do you expect to produce those three lines of output?

    Unless I missed something you don't store those particular strings at any point.

  15. #15
    uhertz is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    How do I close this thread?

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Removing Items In a GUI
    By 67726e in forum New To Java
    Replies: 3
    Last Post: 08-01-2010, 06:46 PM
  2. Apple Items
    By Snatch in forum New To Java
    Replies: 6
    Last Post: 04-05-2010, 12:12 AM
  3. Replies: 7
    Last Post: 12-08-2009, 08:17 PM
  4. HashMap String Return
    By kizilbas1 in forum New To Java
    Replies: 1
    Last Post: 03-10-2008, 04:34 AM
  5. how to return values from hashmap
    By oregon in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 05:56 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
  •