1

Hi guys I have been making a test class for my word puzzle game and the out put is printing the objects reference number to the object. Anyone got the solution to print the return statement of the object.

Output:

Generator stats: word-puzzles generated from words of length 3 [email protected]@b2002fPuzzle.WordPuzzleGenerator@2a4983Puzzle.WordPuzzleGenerator@406199Puzzle.WordPuzzleGenerator@c7b00cPuzzle.WordPuzzleGenerator@1f6f296Puzzle.WordPuzzleGenerator@1df5a8fPuzzle.WordPuzzleGenerator@b2a2d8Puzzle.WordPuzzleGenerator@1e13d52Puzzle.WordPuzzleGenerator@80fa6f

Test Class

public class Test_WordPuzzleGenerator {
    public static void main(String[] args) throws FileNotFoundException {

        int sizeTest1 = 3;
        System.out
                .println("Generator stats: word-puzzles generated from words of length "
                        + sizeTest1);

        for (int i = 0; i < 10; i++) {
            WordPuzzleGenerator puzzle = new WordPuzzleGenerator(sizeTest1);
            System.out.print(puzzle);
        }

        int sizeTest2 = 3;
        System.out
                .println("Generator stats: word-puzzles generated from words of length "
                        + sizeTest2);
        for (int i = 0; i < 10; i++) {
            new WordPuzzleGenerator(sizeTest2);
        }
    }
}

Main program:

public class WordPuzzleGenerator {
    static ArrayList<String> wordList = new ArrayList<String>();

    public WordPuzzleGenerator(int size) throws FileNotFoundException {
        ArrayList<String> puzzleListY = new ArrayList<String>();
        ArrayList<String> puzzleListX = new ArrayList<String>();
        String randomXWord;
        String letterSize = "" + size;
        makeLetterWordList(letterSize);
        boolean finished = false;
        while ( !finished ) {
            finished = true;
            puzzleListX.clear();
            puzzleListY.clear();
            for (int i = 0; i < size; i++) {
                int randomYWord = randomInteger(wordList.size());
                String item = wordList.get(randomYWord);
                puzzleListY.add(item);
            }
            for (int i = 0; i < puzzleListY.size(); i++) {
                StringBuilder sb = new StringBuilder();
                for (int j = 0; j < puzzleListY.size(); j++) {
                    sb.append(puzzleListY.get(j).charAt(i));
                }
                randomXWord = sb.toString();
                if (!wordList.contains(randomXWord) && !puzzleListY.contains(randomXWord)) {
                    finished = false;
                    break;
                }
                puzzleListX.add(randomXWord);

            }




        }
        toString(puzzleListX, puzzleListY);
    }

    public static int randomInteger(int size) {
        Random rand = new Random();
        int randomNum = rand.nextInt(size);
        return randomNum;
    }



    public static void makeLetterWordList(String letterSize) throws FileNotFoundException {

        Scanner letterScanner = new Scanner( new File (letterSize + "LetterWords.txt"));
        wordList.clear();
        while (letterScanner.hasNext()){
            wordList.add(letterScanner.next());
        }
        letterScanner.close();
    }

    public static String toString(ArrayList<String> ArrayList1, ArrayList<String> ArrayList2){
        StringBuilder group1 = new StringBuilder();
        for (int i = 0; i < ArrayList1.size(); i++) {
            group1.append(ArrayList1.get(i) + " ");
        }
        String wordsInString1 = group1.toString();

        StringBuilder group2 = new StringBuilder();
        for (int i = 0; i < ArrayList2.size(); i++) {
            group2.append(ArrayList2.get(i) + " ");
        }
        String wordsInString2 = group2.toString();

        return String.format("\t( %s) ( %s)", wordsInString1, wordsInString2);
    }
}
1
  • also your main program is only an object, your test is the main, you have it kindoff mixed up
    – MRK187
    May 20, 2015 at 6:39

3 Answers 3

8

Your WordPuzzleGenerator class does not override Object's toString. Instead it contains a static toString method with a different signature.

You need a method of this signature in your WordPuzzleGenerator class :

@Override
public String toString()
{
    ...
}

After taking another look, it appers your WordPuzzleGenerator has only static methods and no instance members, so it's unclear what you expect toString to return, or in other words - it's not clear what System.out.print(puzzle); is expected to print.

EDIT:

If you want toString() to print the Lists created in your constructor, you should make them instance members :

ArrayList<String> puzzleListY;
ArrayList<String> puzzleListX;
public WordPuzzleGenerator(int size) throws FileNotFoundException {
    puzzleListY = new ArrayList<String>();
    puzzleListX = new ArrayList<String>();
    ...
}

Then you can override toString like this :

@Override
public String toString()
{
    return WordPuzzleGenerator.toString (puzzleListX,puzzleListY);
}
4
  • I want the toString to return a string version of the contents of both arrays
    – Jackson
    May 20, 2015 at 6:37
  • @Jackson That's what you static toString method appears to be doing, but when you write System.out.print(puzzle); that's not the method being called. You can call it explicitly with System.out.print(WordPuzzleGenerator.toString(list1,list2)), but you'll have to pass the lists to it. If you want to print the lists that you declare inside the WordPuzzleGenerator constructor, you should change those lists to be instance variables instead of local variables.
    – Eran
    May 20, 2015 at 6:42
  • @Jackson Similar to it, but it shouldn't be static.
    – Eran
    May 20, 2015 at 6:49
  • Oh I see now, Thank you for this help it is working now.
    – Jackson
    May 20, 2015 at 6:55
2

you'll have to override the toString method of your objects, since your object inheris from java object

@Override
public String toString(){
\\mystring build up...
return mystring;

notice the override annotation, thats what does the trick ;) happy coding!

3
  • Then how to i reference the overide toString method to insert into my main toString?
    – Jackson
    May 20, 2015 at 6:45
  • The tostring is overriden in the object, in main when you go system.out.print(myobject) java calls that toString method.
    – MRK187
    May 20, 2015 at 6:47
  • And as the other answer suggests, you don't have anything in your object to toString,
    – MRK187
    May 20, 2015 at 6:48
1

try to override 'toString' method in your class as follows:

@Override
public String toString()
{
    //your code
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.