aspose file tools*
The moose likes Beginning Java and the fly likes print random letters(LinkedList) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "print random letters(LinkedList)" Watch "print random letters(LinkedList)" New topic
Author

print random letters(LinkedList)

nick dimi
Greenhorn

Joined: Dec 21, 2011
Posts: 13
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:
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.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Hi Nick, welcome to CodeRanch!

Why not just shuffle the list once, and then in each iteration of the loop remove two letters from the list?
nick dimi
Greenhorn

Joined: Dec 21, 2011
Posts: 13
Stephan van Hulst wrote:Hi Nick, welcome to CodeRanch!

Why not just shuffle the list once, and then in each iteration of the loop remove two letters from the list?


Hi Stephan,i cant understand excactly what you said...can you give me an example?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Well, you have a linked list containing the remaining letters, right? You can fill it with letters in the constructor, and then immediately shuffle it. You only have to shuffle it once. Shuffling it each time you pick a letter is pointless.

Now, what's preventing you from writing a simple for or while loop that takes two letters from the bag?
nick dimi
Greenhorn

Joined: Dec 21, 2011
Posts: 13
Stephan van Hulst wrote:Well, you have a linked list containing the remaining letters, right? You can fill it with letters in the constructor, and then immediately shuffle it. You only have to shuffle it once. Shuffling it each time you pick a letter is pointless.

Now, what's preventing you from writing a simple for or while loop that takes two letters from the bag?


yes this is a very good idea!thanks!i have one more qustion...the letters are printed this way game2.Letters@de6ced,this is my toString public String toString() {
return "Letters_bag{" + "lettersLeft=" + lettersLeft + '}'; ..do you know why this happens?
}
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Because the Letters class does not override toString(). You apparently have some class that does, but Letters does not, so it inherits Object's toString() method.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Welcome to the Ranch

Why are you using a linked list rather than an ArrayList?
nick dimi
Greenhorn

Joined: Dec 21, 2011
Posts: 13
Campbell Ritchie wrote:Welcome to the Ranch

Why are you using a linked list rather than an ArrayList?

because i thought it was easier..:/
nick dimi
Greenhorn

Joined: Dec 21, 2011
Posts: 13
Jeff Verdegan wrote:Because the Letters class does not override toString(). You apparently have some class that does, but Letters does not, so it inherits Object's toString() method.

do you kow how to fix this?because i really dont. .
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Well, you can add a method with the following signature to your Letters class: public String toString()

Have you been through the Java tutorials yet?
nick dimi
Greenhorn

Joined: Dec 21, 2011
Posts: 13
Stephan van Hulst wrote:Well, you can add a method with the following signature to your Letters class: public String toString()

Have you been through the Java tutorials yet?


I read tutorials every time i can..but there are so many things i have to learn about java..
this new toString() is wrong too??hshould i do something different in main?
}
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Well, I assume it's printing allLetters wrong. Take a look at the Arrays.toString() method; in java.util.Arrays.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

nick dimi wrote:
Campbell Ritchie wrote:Welcome to the Ranch

Why are you using a linked list rather than an ArrayList?

because i thought it was easier..:/


If you write your code correctly, you'll never know (except where you instantiate it) whether you're using a LinkedList or an ArrayList, and you'll never care. The code will be identical.

There may still be reasons to choose one over the other--in particular, the big-O performance for your particular usage profile--but ease of coding is not one of them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: print random letters(LinkedList)