0

I've been trying to make a maze type of game by reading in from a configuration file using HashMaps to distinguish between a Room attribute vs Door Attribute. If I just print the String[] values they print the corresponding config values, however, when I try to print the values using a HashMap by calling it by its key, it tells me that the values are null. Any advice?

My code:

     while (file.hasNextLine()) {
            String line = file.nextLine();
            if (line.isEmpty()) {
              continue;
            }
            Scanner scanner = new Scanner(line);
            String type = scanner.hasNext() ? scanner.next() : "";

            String name = scanner.hasNext() ? scanner.next() : "";

            String wall1 = scanner.hasNext() ? scanner.next() : "";

            String wall2 = scanner.hasNext() ? scanner.next() : "";

            String wall3 = scanner.hasNext() ? scanner.next() : "";

            String wall4 = scanner.hasNext() ? scanner.next() : "";

            HashMap<String,String[]> roomSite = new HashMap<String, String[]>();
            HashMap<String,String[]> doorSite = new HashMap<String, String[]>();

            String[] options = new String[]{wall1,wall2,wall3,wall4};

            if(type.equals("room")){
                roomSite.put(name, options);
            }else if(type.equals("door")){
                doorSite.put(name, options);
            }else{
                System.out.println("Error in config file. Please check that all options are valid.");
            }
            System.out.println(roomSite.get(0));
            System.out.println(doorSite.get("d0"));
  }

Output:

null
null
null
null
null
null
null
[Ljava.lang.String;@e9e54c2
null
null

config.ini

room 0 wall d0 wall wall
room 1 d0 wall d1 wall
room 2 wall wall wall d1
door d0 0 1 close
door d1 1 2 open
2
  • 2
    Well, do you have 0 as a key in roomSite or "d0" as a key in doorSite on the iterations when it prints null? Commented Nov 9, 2015 at 21:54
  • 2
    It might make more sense if you use String.split() on your line instead of all those scanners. Commented Nov 9, 2015 at 21:56

2 Answers 2

1

You have System.out.println(roomSite.get(0)); when it should be System.out.println(roomSite.get("0"));

Your maps map from String to String[], not Integer to String[].

Also, if you want to get a more meaningful print statement instead of [Ljava.lang.String;@e9e54c2, you can use the Arrays.toString() method.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, after making the key "0", now it gives me: [Ljava.lang.String;@e9e54c2 followed by nulls, is that normal?
You should initialize your maps before you start reading from your file. Each time you're reading in a line, you're creating a new HashMap. So on the first iteration when you read in room 0, you add its details to the map, which is why you see the [Ljava.lang.String;@e9e54c2 printed. Afterwards, you're creating new maps, and adding the other rooms/doors. The "0" details aren't in the new maps, so you only see null printed on the subsequent iterations.
Ooooh, ok. It works now. Thanks very much for the help! I was stuck on that for like 2 hrs now :)
No problem, glad to help!
1

Initialize your HashMaps before iterating through your file. Try something like this:

public static void main(String []argh) throws FileNotFoundException
{
    Scanner scan = new Scanner(new File("config.ini"));
    HashMap<String, String[]> roomSite = new HashMap<>();
    HashMap<String, String[]> doorSite = new HashMap<>();
    while(scan.hasNext()){
        String type = scan.next();
        String name = scan.next();
        String[] walls = scan.nextLine().trim().split(" ");
        switch(type){
        case "room":
            roomSite.put(name, walls);
            break;
        case "door":
            doorSite.put(name,  walls);
            break;
        default:
            System.out.println("Error in config file. Please check that all options are valid.");
        }
    }
    System.out.println(Arrays.toString(roomSite.get("0")));
    System.out.println(Arrays.toString(doorSite.get("d0")));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.