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

    Default Cannot return values from 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!



  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,271
    Blog Entries
    7
    Rep Power
    24

    Default

    Please stay in one thread for the same problem; don't expect your readers to follow you all over the place; this is your original thread. I'm closing this thread.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Cannot return items from a hashmap
    By uhertz in forum New To Java
    Replies: 15
    Last Post: 06-20-2011, 02:06 AM
  2. how to get the values from hashmap
    By baktha.thalapathy in forum New To Java
    Replies: 5
    Last Post: 05-25-2010, 02:12 PM
  3. HashMap contains all values but doesn't show all values
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 05-10-2009, 11:35 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, 04: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
  •