1

The problem is as follows: 6 words are to be displayed on the screen. These words are chosen at random from a list. When I wrote the code, I didn't get any error, but when I ran it in eclipse, I got the following gibberish result in the console "package.wordsContainer@659e0bfd".

What did I do wrong?

public class wordsContainer {
    Collection<String> wordList = new ArrayList<String>();

    public void wordGroup1() {
        wordList.add("Ant");
        wordList.add("Almond");
        wordList.add("Atom");
        wordList.add("Affair");
        wordList.add("Ample");
        wordList.add("Blue");
        wordList.add("Black");
        wordList.add("Bronze");
        wordList.add("Beauty");
        wordList.add("Beautiful");
        wordList.add("Batter");
        wordList.add("Crazy");
    }


    public Collection<String> getRandomWords() {
        wordGroup1();
        LinkedList<String> wordLinkedList = new LinkedList<String>(wordList);
        ArrayList<String> subList = new ArrayList<String>();

        int i = 0;
        while (i < 6) {
            int index = (int) Math.random() * 10;
            if (!subList.contains(wordLinkedList.get(index))) {
                subList.add(wordLinkedList.get(index));
                i++;
            }
        }
        return subList;
    }
}



public class wordsContainerTest {
    public static void main(String[] args) {
        wordsContainer list1 = new wordsContainer();

        list1.wordGroup1();

        System.out.println(list1);
        System.out.println(list1.getRandomWords());

    }
}
cocoatomo
  • 5,432
  • 2
  • 14
  • 12
Frosty619
  • 1,381
  • 4
  • 23
  • 33
  • The toString method is not smart enough to properly print a string collection. – Tarik Aug 29 '15 at 02:12
  • Override tostring method. And also it's not gibberish. Its displaying object value as hash code. – Pavan Aug 29 '15 at 02:51
  • Thanks guys, but I dont exactly know what to put in the toString method. Every other time I had to override the toString method, I simply put the name down (return this.name), How would I go about overriding this method for lists? – Frosty619 Aug 29 '15 at 02:55
  • So I have been looking everywhere, and I tried the following: public String ToString(List> list) { String result = " "; for (int i = 0; i < list.size(); i++) { result += " " + list.get(i); } return result; When I run the code, the console displays the same result – Frosty619 Aug 29 '15 at 03:15

2 Answers2

6

It's not gibberish, hexadecimal representation of the hash code of the object wordsContainer

That result is from the line

   System.out.println(list1); //wordsContainer 

Not from ArrayList.

In order to work properly you need to override toString method in your class wordsContainer

To understand what exactly is "package.wordsContainer@659e0bfd" read the answer I wrote long back.

https://stackoverflow.com/a/17878495/1927832

Apart from that, please follow java naming conventions, Class names starts with Capital letter.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Thank you for the quick response and clearing up a lot of confusion. I am going to see if I can figure out what to put in the toString method before asking for more help. – Frosty619 Aug 29 '15 at 02:38
  • @user2430656 Fine, here is the start :http://stackoverflow.com/questions/3946529/what-is-the-best-standard-style-for-a-tostring-implementation – Suresh Atta Aug 29 '15 at 02:45
  • @user2430656 Mark this answer if it is really helpful to you :) – Suresh Atta Aug 29 '15 at 02:47
  • Thank you ,it really was (: I have been searching for the past 30 minutes as to how I should go about override the toString method. I am at a loss of how to display the lists. All the examples that I looked at dont seem to help. A penny for your thoughts? – Frosty619 Aug 29 '15 at 02:50
  • @sᴜʀᴇsʜ ᴀᴛᴛᴀ I have been looking everywhere, and I tried the following: public String ToString(List> list) { String result = " "; for (int i = 0; i < list.size(); i++) { result += " " + list.get(i); } return result; Now, when I run the application, the console displays nothing. :( – Frosty619 Aug 29 '15 at 03:15
  • apologies, I meant to say it displays the same result as before – Frosty619 Aug 29 '15 at 03:21
0
 System.out.println(list1); //wordsContainer 

You can't print out objects directly, you will just print out the reference to the place in memory where the object is saved, which is that weird output you are getting. You have to override the toString() method in your object or print out the properties of the object that you want individually.

Zarwan
  • 5,537
  • 4
  • 30
  • 48