0

So I am adding values into a hash map and then printing out the hash map. My Values of the hash map seem to print out fine but for some reason my keys printed aren't the same as the keys I input. Here is the relevant code:

//adds values into the hash map
public void addTransition(int curState, char curBit, int newState, char newBit, int direction){
   Trigger trigger = new Trigger(newState, newBit);
   Action action = new Action(newState, newBit, direction);
   program.put(trigger,action);
   System.out.println(program);
}

Edit:Updated the trigger class

//the trigger class
class Trigger{
    public char curBit;
    public int curState;
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + getOuterType().hashCode();
        result = prime * result + curBit;
        result = prime * result + curState;
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Trigger other = (Trigger) obj;
        if (!getOuterType().equals(other.getOuterType()))
            return false;
        if (curBit != other.curBit)
            return false;
        if (curState != other.curState)
            return false;
        return true;
    }
    private TuringMachine getOuterType() {
        return TuringMachine.this;
    }
    public Trigger(int x, char y){
        curState = x;
        curBit = y;
    }
    @Override
    public String toString(){
        return "|" + curState + ", " + curBit + "|";
    }
}

So for example, if I do addTransition(0,'1',0,'1',1), it is printing out {(0, 49)=(0, 1, 1)}.

Edit: So for the above one, its printing out (0,1) = (0,1,1) as expected But if I do addTransition(0,'0',1,'0',1) after adding the previous 0,1,0,1,1 its giving me: {|0, 1|=(0, 1, 1), |1, 0|=(1, 0, 1)}

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • 1
    What makes it think it is related to HashMap? before you blame HashMap, have you tried to simply print out your `Trigger` and `Action` directly? – Adrian Shum Apr 24 '15 at 02:30

1 Answers1

4

You are passing a char into a constructor that takes an int and it is getting implicitly converted.

The character code for '1' is 49.

Mike Tunnicliffe
  • 10,674
  • 3
  • 31
  • 46