0

So I have a map that takes in key as Trigger and value as Action. Trigger is an inner class that takes in two parameters. Action is also an inner class that takes in three parameters. I have a addTransition method that takes in 5 parameters, the first two being used for trigger and the latter 3 for action. In this method I have added a system.out.println(map); but for some reason, its printing out this:

{meta.TuringMachine$Trigger@7b3d091c=meta.TuringMachine$Action@ec636273}
{meta.TuringMachine$Trigger@7b3d091c=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d08fe=meta.TuringMachine$Action@ec636273}
{meta.TuringMachine$Trigger@7b3d091c=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d08fe=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d0900=meta.TuringMachine$Action@ec636273}
{meta.TuringMachine$Trigger@7b3d091c=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d08ff=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d08fe=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d0900=meta.TuringMachine$Action@ec636273}
{meta.TuringMachine$Trigger@7b3d091c=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d08ff=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d08fe=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d0900=meta.TuringMachine$Action@ec636273}
{meta.TuringMachine$Trigger@7b3d091c=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d08ff=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d08fe=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d0900=meta.TuringMachine$Action@ec636273, meta.TuringMachine$Trigger@7b3d0920=meta.TuringMachine$Action@ec636273}

Edit: Doesn't seem like my newBit is empty. Also this is the method where I am adding values to 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);
       }
TuringMachine a = new TuringMachine();
           a.addTransition(0,'1',0,'1',1);

For some reason this is printing

{(0, 49)=(0, , 0)}

1 Answers1

0

You did not override the toString() method in Trigger and Action, so when you try to print an object from one of these classes it will call Object.toString() instead.

From the documentation:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

What you should do is something like this:

class Trigger {
    private int state;
    private char curBit;

    // rest of the code

    @Override
    public String toString() {
        return "(" + this.state + ", " + this.curBit + ")";
    }
}
Community
  • 1
  • 1
Anderson Vieira
  • 8,919
  • 2
  • 37
  • 48
  • So for example in my Trigger class, I have two fields: int state, char curBit. I would do public String toString(){ return state + curBit;}. But thats a type mismatch because I cannot convert from int to string – theProdigyLebron Apr 23 '15 at 23:44
  • For the trigger, its only printing out two values. The trigger class has int newState, char newBit, int direction and my toString method is return "(" + this.newState + "," + this.newBit + "," + this.direction+ ")"; – theProdigyLebron Apr 23 '15 at 23:51
  • @theProdigyLebron What's the output you're getting? Check if `newBit` is empty. – Anderson Vieira Apr 24 '15 at 00:06
  • @theProdigyLebron If `newBit` is the mid value in `(0, , 0)` it is probably not set. – Anderson Vieira Apr 24 '15 at 00:14
  • Oh I had just messed up my constructor. But for some reason its printing out different values then expected. I updated my question – theProdigyLebron Apr 24 '15 at 00:33
  • @theProdigyLebron It's hard to say anything else without the code for the constructors, the `toString()` methods, and knowing what the expected values are. – Anderson Vieira Apr 24 '15 at 00:41