Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Cannot get values from hashmap

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cannot get values from hashmap

    I put stuff in a hashmap but cannot retrieve it
    <code>
    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; Yesterday at 03:32 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Cannot get values from hashmap

    For future references, please use the code tags - its difficult to help when we can't even read the code

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/45430-cannot-return-items-hashmap.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


Similar Threads

  1. [SOLVED] Should i use a hashmap?
    By 6Sloth9 in forum Collections and Generics
    Replies: 2
    Last Post: May 1st, 2011, 08:58 PM
  2. TreeMap vs HashMap
    By Kerr in forum Collections and Generics
    Replies: 7
    Last Post: March 10th, 2011, 10:12 AM
  3. hashmap & arraylist mapping
    By abhi178 in forum Collections and Generics
    Replies: 3
    Last Post: November 29th, 2010, 07:32 AM
  4. Problem in HashMap
    By Hikari9 in forum Collections and Generics
    Replies: 2
    Last Post: April 19th, 2010, 10:44 PM
  5. Bitstrings vs Hashmap
    By April in forum Collections and Generics
    Replies: 3
    Last Post: February 2nd, 2010, 11:56 AM