Some Guy
Some Guy

Reputation: 401

Having trouble with using Hashmaps in java

In a program, I have to read in three lines of text. Each line has one space, with two objects on either side. The first is a string (an operator) and the second is a number (number). After reading in this 3 lines, I then put them into a hashmap to be later used.

I had a automatic method of adding the lines but for the purpose of trying to debug this problem I had to add the lines info manually into the map. Here is my current code:

String line1 = in.nextLine();
        String line2 = in.nextLine();
        String line3 = in.nextLine();

        System.out.println(line1 + " " + line2 + " " + line3);
        map.put(line1.split(" ")[0],
                Integer.parseInt(line1.split(" ")[1]));
        map.put(line2.split(" ")[0],
                Integer.parseInt(line2.split(" ")[1]));
        map.put(line3.split(" ")[0],
                Integer.parseInt(line3.split(" ")[1]));
        if (in.hasNextLine())
            in.nextLine();
        System.out.println("Size: " + map.size());

The first print statement (line1 + line2 + line3) prints the correct reading input that the map should have, each time:

* 4 - 3 / 2

It's then put into the map, then have calculations done, then printed out some true/false statements (in context with the program

* 4 - 3 / 2
Size: 3
- 3
Found solution for - 3. It's 1 4
/ 2
Found solution for / 2. It's 2 5
* 4
Not Possible.

As you can see, my program correctly goes through the map and has 3 entries (- 3, / 2, and * 4) Now back to the top, where we want three more lines. Again here is the out statement of the three lines: + 5 + 6 - 2 Size: 2 - 2 Found solution for - 2. It's 1 3 + 6 Found solution for + 6. It's 2 4 Not Possible.

What?? Same code - Read in 3 lines, RECOGNIZED 3 lines, but the first map.put did not actually put in the objects to the map. To put this into more perspective, the program goes like this:

3 lines are read in (format: 'operator number')
You are to use the numbers 1-6 and the operator given to make the number. BUT, you can only use   each number (1-6) once in the 3 equations. The first one is fine:
* 4 - 3 / 2
Size: 3
- 3
Found solution for - 3. It's 1 4
/ 2
Found solution for / 2. It's 2 5
* 4
Not Possible.
For the first I have to use subtraction to get three. 4 - 1 is 3 so that's a pair. Now I need to use division to get 2, but I can't use 1 and 4. 6 / 3 = 2 so 6 and 3 are a pair. You get it.

here's my current code: Sorry for the sloppiness... Thanks for any help...

KenKen.dat file

Upvotes: 0

Views: 85

Answers (1)

A4L
A4L

Reputation: 17595

The next three lines are

+ 5
+ 6
- 2

and you use the operator as the key for the map entry. The javadocs of the Map interface states about duplicate key

An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

So first you put 5 whith the key +, then comes 6 with as key + too, here the first mapping is overridden. your map contains at the end of this reading step only two entries {+, 6} and {-, 2} hence the size of 2.

I doubt HashMap is the data structure you may want to use in this case. Instead create a class KenKenPair where you store each operand and operator found in a line

class KenKenPair {
    final String operator;
    final String operand;
    public KenKenPair(String operator, String operand) {
        this.operator = operator;
        this.operand = operand;
    }

    public String getOperator() { return operator; }
    public String getOperand() { return operand; }
}

and use

 List<KenKenPair> list = new ArrayList<>();

to collect them all, then iterate over the List instead of the entry set.

Besides of that, be careful about your choices for the collections you want to use, for example entries in a WeakHashMap may be removed without you doing so explicitly.

The behavior of the WeakHashMap class depends in part upon the actions of the garbage collector, so several familiar (though not required) Map invariants do not hold for this class. Because the garbage collector may discard keys at any time, a WeakHashMap may behave as though an unknown thread is silently removing entries. In particular, even if you synchronize on a WeakHashMap instance and invoke none of its mutator methods, it is possible for the size method to return smaller values over time, for the isEmpty method to return false and then true, for the containsKey method to return true and later false for a given key, for the get method to return a value for a given key but later return null, for the put method to return null and the remove method to return false for a key that previously appeared to be in the map, and for successive examinations of the key set, the value collection, and the entry set to yield successively smaller numbers of elements.

Also there is no need to split the same line multiple times, instead split onces and assign the result to a local variable:

String parts[] = line1.split(" ");
list.add(new KenKenPair(parts[0], parts[1]));

Upvotes: 3

Related Questions