Results 1 to 5 of 5
Thread: print random letters(LinkedList)
- 12-21-2011, 06:34 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
print random letters(LinkedList)
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:
Letters:Java 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(); } }
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.Java 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; }Last edited by pbrockway2; 12-21-2011 at 06:58 PM. Reason: code tags added
- 12-21-2011, 07:13 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
So what problem are you having? There seem to be various aspects to this: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").
* 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, 09:14 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
Re: print random letters(LinkedList)
Thnks for your answer.Itried this and i think it works..
and in main:Java Code:Letters getNextLetter(int k) { Collections.shuffle(lettersLeft); return lettersLeft.removeFirst(); }
but it prints something like this 'game2.Letters@de6ced',this is my toStringJava Code:for (int i=0;i<7;i++){ System.out.println( sb.getNextLetter(i)); }..do you know why this happens?Java Code:public String toString() { return "Letters_bag{" + "lettersLeft=" + lettersLeft + '}';}Last edited by pbrockway2; 12-21-2011 at 09:20 PM. Reason: changed closing code tags to use /
- 12-21-2011, 09:25 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: print random letters(LinkedList)
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.)it prints something like this 'game2.Letters@de6ced'
Java 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; } }Last edited by pbrockway2; 12-21-2011 at 09:32 PM.
- 12-21-2011, 09:31 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Help with random letters.
By jenxin in forum New To JavaReplies: 110Last Post: 02-28-2011, 03:32 PM -
Getting random word to print out in an Array
By manifest3r in forum New To JavaReplies: 2Last Post: 02-16-2011, 10:46 PM -
generate random letters inbetween a string
By greg677 in forum New To JavaReplies: 1Last Post: 05-04-2010, 05:06 AM -
Generate a random code 4 letters long
By bl00dr3d in forum New To JavaReplies: 9Last Post: 04-06-2009, 05:32 AM -
need help print random digital
By lowpro in forum New To JavaReplies: 1Last Post: 12-31-2007, 06:47 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks