# print random letters(LinkedList)

• 12-21-2011, 07:34 PM
nickdimi
hi everyone..
In this program i created a LinkedList('lettersLeft') in which I add Letters('Letters' is a class).Each letter has 3 values :1)which is that letter(ie 'A'),2)the times it appears in the game(ie '9'),3)the points you get (ie '1').Also, with getNextLetter() i get the next random letter from the bag. I want to make a for loop that will print me 2 random letters from the bag with their values(ie "A,9,1 D,4,2"). This is my code(if you dont understand something ask)
Letters_bag:

Code:

```public class Letters_bag { public static final Letters A = new Letters('a', 9, 1); public static final Letters B = new Letters('b', 2, 3); public static final Letters C = new Letters('c', 2, 3); public static final Letters D = new Letters('d', 4, 2); public static final Letters E = new Letters('e', 12, 1); public static final Letters F = new Letters('f', 2, 4); public static final Letters[] allLetters = new Letters[] {     Letters_bag.A,     Letters_bag.B,     Letters_bag.C,     Letters_bag.D,     Letters_bag.E,     Letters_bag.F, }; LinkedList<Letters> lettersLeft = new LinkedList(); public Letters_bag() {     // add all the letters     addLetter(A);     addLetter(B);     addLetter(C);     addLetter(D);     addLetter(E);     addLetter(F); } // helper method to add the letters private void addLetter(Letters sl) {     for (int i=0;i<sl.getCount();i++) {         this.lettersLeft.add(sl);     } }  /**  *Returns the next random letter from the bag.  */ Letters getNextLetter() {     // shuffle those letters     Collections.shuffle(lettersLeft);     // return a random letter     return lettersLeft.removeFirst(); } }```
Letters:
Code:

```public class Letters { private char value; private int count; private int points; public Letters(char value, int count, int points) {     this.value = value;     this.count = count;     this.points = points; } public char getValue() {     return value; } public int getCount() {     return count; } public int getPoints() {     return points; }```
I think it is something with the Collections.shuffle but i cant figure out how to put it in a for loop.Any ideas are welcome.
• 12-21-2011, 08:13 PM
pbrockway2
Re: print random letters(LinkedList)
Hi nickdimi, welcome to the Forums.

When you post code use the "code" tags. Put [code] at the start of the code and [/code] at the end. This will mean that the code ends up properly formatted.

Quote:

I want to make a for loop that will print me 2 random letters from the bag with their values(ie "A,9,1 D,4,2").
So what problem are you having? There seem to be various aspects to this:
* having the for loop do something twice
* inside the for loop, obtaining an instance of Letters
* printing the Letters obtained

Collections.shuffle() is a good way to randomise the list, but it only need happen once, for example in the constructor after the list contents have been added.
• 12-21-2011, 10:14 PM
nickdimi
Re: print random letters(LinkedList)
Thnks for your answer.Itried this and i think it works..
Code:

``` Letters getNextLetter(int k) {         Collections.shuffle(lettersLeft);       return lettersLeft.removeFirst();     }```
and in main:
Code:

``` for (int i=0;i<7;i++){             System.out.println(  sb.getNextLetter(i));                 }```
but it prints something like this 'game2.Letters@de6ced',this is my toString
Code:

```public String toString() { return "Letters_bag{" + "lettersLeft=" + lettersLeft + '}';}```
..do you know why this happens?
• 12-21-2011, 10:25 PM
pbrockway2
Re: print random letters(LinkedList)
Quote:

it prints something like this 'game2.Letters@de6ced'
What you are seeing is the default Java way of printing an object. You need to provide Letters with a toString() that provides a nicer string. (Or have the Letters_bag toString() do the formatting itself using the Letters getter methods to obtain what it wants to print.)

Code:

``` import java.util.Collections; import java.util.LinkedList; public class FooList {     private LinkedList<Foo> list;         FooList() {         list = new LinkedList<Foo>();         list.add(new Foo(666));         list.add(new Foo(42));         list.add(new Foo(-1));         Collections.shuffle(list);     }             public String toString() {         return "FooList{the list is " + list + "}";     }             public static void main(String[] args) {         System.out.println(new FooList());     } } class Foo {     int value;     Foo(int v) {value = v;}             public String toString() {         return "value=" + value;     } }```
• 12-21-2011, 10:31 PM
nickdimi
Re: print random letters(LinkedList)
i cant understand how toString is really working..anyway this is what the IDE generates
Code:

``` public String toString() {         return "Letters_bag{" + "lettersLeft=" + lettersLeft + '}';     }```
is it wrong?