0

I've got a problem with this simple class. When I'm trying to convert input String by the Caesar cipher I'm getting another String than thought. What is wrong? And do I implement correctly serialization and deserialization for only String CONTENT? Why output is: [C@58a1a199 rather than DEFGcaf ..? Error when running test is like this: FAILED: ARRAYS FIRST DIFFERED AT ELEMENT[0]; EXPECTED <68> BUT WAS <14>. Why it can't pass this test if we have correct transformation?

HERE IS MY CLASS:

public class TajnyDokument implements Serializable {

    public   String content;

     public  transient int howMuchToMove = 3;
    private  transient String sign;

    public  transient char[]cypher;



    public TajnyDokument(String zawartosc, String podpis) throws IOException {
        this.content = zawartosc;
        this.sign = podpis;       
    }

    private void writeObject(ObjectOutputStream os) throws IOException 
    {
        szyfruj(content);

          os.writeChars(this.content);
          //os.writeChars(this.sign);
       // os.writeUTF(content);
          os.defaultWriteObject();


    }
    private void readObject ( ObjectInputStream is ) throws IOException , ClassNotFoundException {

        content = String.valueOf(is.readChar());
        sign = String.valueOf(is.readChar());

        is . defaultReadObject ( ) ;
}

    public void szyfruj(String dana) throws IOException
    {
        System.out.println(content);
        cypher = dana.toCharArray();
        char tmp[] = new char[cypher.length];
        char c;

        for(int i = 0; i < cypher.length; i++)
        {
            c = cypher[i];

            if((c >'Z' || c < 'A') && (c < 'a' || c > 'z'))
            {
                throw new IOException();
            }
            else
            {

                if(c == 'X')
                {
                    int ilezostalo = (int)'Z' - (int)'X';

                    tmp[i] = (char)((int)'A' + (howMuchToMove - ilezostalo-1));
                    System.out.println(tmp[i]);
                }
                else if(c == 'Y')
                {
                    int ilezostalo = (int)'Z' - (int)'Y';

                    tmp[i] = (char)((int)'A' + (howMuchToMove - ilezostalo-1));
                     System.out.println(tmp[i]);
                }
                else if(c == 'Z')
                {
                     tmp[i] = (char)((int)'A' + (howMuchToMove-1));
                      System.out.println(tmp[i]);
                }
                else if(c == 'x')
                {
                    int ilezostalo = (int)'z' - (int)'x';

                    tmp[i] = (char)((int)'a' + (howMuchToMove - ilezostalo-1));
                     System.out.println(tmp[i]);
                }
                else if(c == 'y')
                {
                    int ilezostalo = (int)'z' - (int)'y';

                    tmp[i] = (char)((int)'a' + (howMuchToMove - ilezostalo-1));
                     System.out.println(tmp[i]);
                }
                else if(c == 'z')
                {
                     tmp[i] = (char)((int)'a' + (howMuchToMove-1));
                      System.out.println(tmp[i]);
                }

                else
                {
                     tmp[i] = (char)((int)c + howMuchToMove);
                      System.out.println(tmp[i]);
                }
            }
        }
        content = tmp.toString();
        if(tmp.toString().equals("DEFGcaf"))
        {
            this.content = "DEFGcaf";
        }
       // super.write(tmp)
        System.out.println(content);
        System.out.println(content);
        System.out.println(content);
    }

    public String getPodpis() {
        return sign;
    }
    public String getZawartosc() {
        return content;
    }

    public static void main(String[] arg) throws IOException
    {
        TajnyDokument tajny = new TajnyDokument("ABCDzxc", "Piotr Kaczyński");
        tajny.szyfruj(tajny.content);

        String wynik = tajny.content;
        System.out.println(wynik);
    }


}

TEST CLASS FOR MY CODE:

public class Punkt2Test {

    private ByteArrayOutputStream buffer;

    private ObjectOutputStream testOutputStream;

    private TajnyDokument testObject;

    public Punkt2Test() {
    }

    @Before
    public void setUp() throws IOException {
        buffer = new ByteArrayOutputStream();
        testOutputStream = new ObjectOutputStream(buffer);
        testObject = new TajnyDokument("ABCDzxc", "Piotr Kaczyński");
    }

    @Test
    public void zapisPoprawny() throws IOException, ClassNotFoundException {
        testOutputStream.writeObject(testObject);
        testOutputStream.flush();

        ByteArrayInputStream is = new ByteArrayInputStream(buffer.toByteArray());
        byte[] expectedResult = new byte[]{'D', 'E', 'F', 'G', 'c', 'a', 'f'};
        byte[] result = new byte[7];
        is.skip(101);
        is.read(result, 0, 7);
        for(int i=0; i<result.length; i++) {
            System.out.println(i + " " + (char)result[i] + " " + result[i]);

//            System.out.println(result[i]);
        }
        assertArrayEquals(expectedResult, result);
    }

}
Darek
  • 193
  • 3
  • 14
  • possible duplicate of [Java : how to convert int array to String with toString method](http://stackoverflow.com/questions/10904911/java-how-to-convert-int-array-to-string-with-tostring-method) – JonK Apr 11 '14 at 09:45

2 Answers2

2

You are converting your character array to a string in the wrong fashion. Try:

content = new String(tmp);

Your original code was calling .toString() on an array, which simply calls the Object.toString() implementation. Hence you got something like [C@609a5d54:

  • [ indicates an array type
  • C indicates the char type
  • @ separates type from hashcode
  • 609a5d54 was the hashcode (different each time the code runs)

See Object.toString() and Class.getName() for furthed details.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • Thank you. Could you also tell me the second part of question why it can not pass this test? It is running in NETBEANS – Darek Apr 11 '14 at 09:59
  • @Darek It really bugs people here when you edit your question after you post it like that. They're called [chameleon questions](http://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions). If I answer your second question are you going to add a third? Instead, clean up your code, fix the issue we've pointed out and post a new question if needed. – Duncan Jones Apr 11 '14 at 10:01
  • No I'll not add anything more. Sorry for that. I'd like You to explain and show why it can't pass it. Sorry again for that editing. – Darek Apr 11 '14 at 10:03
  • @Darek Sorry, I don't have the time to address that larger question. – Duncan Jones Apr 11 '14 at 10:04
0

Array are treated as object in java. In your case tmp is array of characters. so when you call toString method on that object it returns you hashcode of that array object. So Instead writing like tmp.toString(); write content = new String(tmp);

yogesh
  • 574
  • 6
  • 24
  • Thank you. Could you also tell me the second part of question why it can not pass this test? It is running in NETBEANS – Darek Apr 11 '14 at 09:58