-1

When do test of scramble() method it actually returns something like this:

public class Scramble_game {    
   public static void main(String[] args) 
   {
      System.out.println("START");
      Word name = new Word("iskander");
      System.out.println(name.getWord() + "  " + name.getScrambleWord());
   }
}

my output is always like this:

START
[C@57f530d8 [C@2259e205

public class Word 
{
   char[] word;
   char[] scrambleWord;
   boolean[] letters;
   boolean correct;

   public Word(String wordName)
   {
      this.word = wordName.toCharArray();
      this.letters = new boolean[wordName.length()];
      this.correct = false;        
      this.scrambleWord = this.scramble();
   }

   public char[] getScrambleWord(){return this.scrambleWord;}
   public char[] getWord(){return this.word;}

   public int getIndex()
   {
      Random rnd = new Random();
      int i = rnd.nextInt(this.word.length);
      if(this.letters[i] == true)
      {
         return getIndex();
      }
      this.letters[i] = true;
      return i;
   }

   public char[] scramble()
   {
      char[] temp = new char[this.word.length];
      for(int i = 0; i < this.word.length; i++)
      {
         char tempChar = word[this.getIndex()];
         temp[i] = tempChar;
      }
      return temp;
   }
}

As I understood this strange words are the addresses of objects? I might be wrong, can you explain me what are these strange letters and where is my code problem.

Tom
  • 16,842
  • 17
  • 45
  • 54
  • I don't know nothing about Java, but I think you're supposed to use `StringBuilder` when creating a string like this. – Sergio Tulentsev May 25 '15 at 16:49
  • possible duplicate of [What's the simplest way to print a Java array?](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – Tom May 25 '15 at 16:55

3 Answers3

3

If you want to print a char[] as a String, you need to manually convert it, like so:

public String getWord() { return new String(this.word); }
Armand
  • 23,463
  • 20
  • 90
  • 119
0

If you're attempting to print a Java object, the virtual machine tries to invoke a toString() method of this object. Since Word hasn't overriden a toString() method, java will invoke the default toString() imlementation of Object class, which is:

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

This method basically print the object "adress". If you want to print something more user friendly you would need to override a toString method (if it's your class) or manually extract what you are interested in.

dstronczak
  • 2,406
  • 4
  • 28
  • 41
  • This is not wrong, but not helpful here, because OP doesn't print a `Word` instance. – Tom May 25 '15 at 17:08
0

In order to convert char[] to String String.valueOf(charArray) have to be used instead of toString() method, which will return the object's address as it was previously mentioned.

char[] customCharArray = {'m','y',' ','a','r','r','a','y'};
String requiredString = String.valueOf(customCharArray);
System.out.println(requiredString);

Output: my array
AndreyFr
  • 3
  • 3