Results 1 to 16 of 16
- 06-17-2011, 07:12 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
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 04:32 PM.
- 06-17-2011, 07:50 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Returned to where? Perhaps you could post the code that generated that output, and say what output you were expecting.All of the rooms returned are null
- 06-17-2011, 08:08 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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]
- 06-17-2011, 02:17 PM #4
Better would be to add an id so you know which line did the printing:System.out.println(" " + west);
System.out.println("west= " + west);
Looks like in the Room constructor you create a new EMPTY exits object.Java Code:exits=new HashMap<String, Room>();
- 06-17-2011, 05:29 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
Hi
I do fill the exits object. I've added a whole whack of code in the original post
- 06-17-2011, 05:32 PM #6
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.
- 06-17-2011, 05:46 PM #7
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
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
- 06-17-2011, 05:49 PM #8
Better get used to it. Programming takes a lot of attention to details.This is a real pain in the ass
- 06-18-2011, 02:32 AM #9
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
No luck at all. Seems that the hashmap is not being populated
- 06-18-2011, 02:36 AM #10
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
Can I close this and post to new trhead?
- 06-18-2011, 02:51 AM #11
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!
- 06-18-2011, 03:11 AM #12
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
"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!
- 06-18-2011, 03:15 AM #13
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.
What values?the values are not being stored in the hashmap
Where do you put them in the hashmap?
- 06-18-2011, 03:19 AM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
What line(s) of your code do you expect to produce those three lines of output?The expected result for the first three lines is:
south theater
east lab
west pub
Unless I missed something you don't store those particular strings at any point.
- 06-20-2011, 01:35 AM #15
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
How do I close this thread?
-
Similar Threads
-
Removing Items In a GUI
By 67726e in forum New To JavaReplies: 3Last Post: 08-01-2010, 05:46 PM -
Apple Items
By Snatch in forum New To JavaReplies: 6Last Post: 04-04-2010, 11:12 PM -
How to create a new HashMap from a HashMap entries of other methods
By pandeyalok in forum Advanced JavaReplies: 7Last Post: 12-08-2009, 07:17 PM -
HashMap String Return
By kizilbas1 in forum New To JavaReplies: 1Last Post: 03-10-2008, 03:34 AM -
how to return values from hashmap
By oregon in forum New To JavaReplies: 2Last Post: 08-01-2007, 04:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks