Results 1 to 5 of 5
  1. #1
    nickdimi is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default 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:

    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();
    }
    
    }
    Letters:
    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;
    }
    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.
    Last edited by pbrockway2; 12-21-2011 at 07:58 PM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default 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.

    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.

  3. #3
    nickdimi is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: print random letters(LinkedList)

    Thnks for your answer.Itried this and i think it works..
    Java Code:
     Letters getNextLetter(int k) {
            Collections.shuffle(lettersLeft);
           return lettersLeft.removeFirst();
        }
    and in main:
    Java 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
    Java Code:
    public String toString() { 
    return "Letters_bag{" + "lettersLeft=" + lettersLeft + '}';}
    ..do you know why this happens?
    Last edited by pbrockway2; 12-21-2011 at 10:20 PM. Reason: changed closing code tags to use /

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default Re: print random letters(LinkedList)

    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.)

    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 10:32 PM.

  5. #5
    nickdimi is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: print random letters(LinkedList)

    i cant understand how toString is really working..anyway this is what the IDE generates
    Java Code:
     public String toString() {
            return "Letters_bag{" + "lettersLeft=" + lettersLeft + '}';
        }
    is it wrong?

Similar Threads

  1. Help with random letters.
    By jenxin in forum New To Java
    Replies: 110
    Last Post: 02-28-2011, 04:32 PM
  2. Getting random word to print out in an Array
    By manifest3r in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 11:46 PM
  3. generate random letters inbetween a string
    By greg677 in forum New To Java
    Replies: 1
    Last Post: 05-04-2010, 06:06 AM
  4. Generate a random code 4 letters long
    By bl00dr3d in forum New To Java
    Replies: 9
    Last Post: 04-06-2009, 06:32 AM
  5. need help print random digital
    By lowpro in forum New To Java
    Replies: 1
    Last Post: 12-31-2007, 07:47 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •