• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to randomly select a string in an array based on a corresponding number  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to generate a random word from the array that I have made including the words by making it corresponding with a randomly generated number. So for example, if the number generated is 0, then the word that the person has to guess would be "AUNT". How would I transfer the randomly generated number from one method into the array method to get the word the person has to guess?

Btw this is part of an assignment, the description is for reference.


Write a program called Word Guessing Game. Open the file FourLetterWords.txt and write the contents into an array of Strings (the file has 87 words in it). Then use a randomly generated number between 0 and 86 to select a word. The player will then try to guess the word selected by the game. The player is allowed 7 tries, if the player does not guess the word on the 7th try he/she losses. Display the letter of the word as they are guessed in the correct order, you will also display the incorrect letters. The game is over when:  The player completes the word, or guesses the whole word correctly.  The player does not guess the word in seven tries.
The player must also be allowed to terminate the game.
The game must have at least 5 classes:
 Main Class
 Class to return a random integer between 0 and 86.
 Class to return a populated array of 87, 4 letter words.
 Class to return a character that the player enters from the keyboard.
 Class to display both the correctly guessed letters and the incorrect letters.



My code (it is not complete, my attempt to do what I am trying to do is obviously not working.)

 
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without giving the answer away, given any array, how would you loop through it to output its contents?

The idea is the same just without the loop.

Once you know the answer, your case statement inside the switch statement would become a one liner.

Edit: don't forget the break after the case!
 
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java coding conventions are that the use of all capital letters for variable names should be restricted to final static variables (ie constants).

Now you don't have to use the coding conventions but you'll find it much easier to get help if you do use them as it is easier for other people to read your code.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didnt understand why you want to use switch when you can simply use
 
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Magic values in a source like "87" are not nice. This is nicer and less prone to error should you add more elements:


Also java.util.Random has a method

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!