Help with a ramdom word selection

Hello everyone.

I'm trying to write a ramdom word program using a arraylist.

I'm NOT a student, I'm doing this as a fun project and to learn how to use ramdom method..

code is below.

class WordClass1{

private String wd_id;

private String word_name;

public WordClass1(String id){

wd_id = id;

}

public WordClass1(String id, String Wd){

this.wd_id = id;

this.word_name = Wd;

}

// accessors

public String getWd_id(){return wd_id;}

public String getWord(){return word_name;}

public String toString(){

return"(" + wd_id + word_name +")";

}

}//close class Person class

publicclass HangmanWords{

static ArrayList<WordClass1> arlist;

static Scanner kbd;

publicstatic WordClass1 makePerson(){

WordClass1 temp =null;

// prompt for data

String id;

String Wd;

System.out.print("Enter ID Number ==>");

id = kbd.next();

System.out.print("Enter Last Name ==>");

Wd = kbd.next();

// make an object

temp =new WordClass1(id, Wd);

return temp;

}

publicstaticvoid main(String[] args){

// make array list object

List < WordClass1 > arlist =new ArrayList < WordClass1 > ();

arlist.add(new WordClass1("A1","STRING"));

arlist.add(new WordClass1("A2","PERSON"));

arlist.add(new WordClass1("B1","CLASS"));

arlist.add(new WordClass1("B2","JAVA"));

System.out.println(arlist);

// make a scanner

kbd =new Scanner(System.in);

int choice;

System.out.println("Make a Section: ");

System.out.println("1. Enter Word ");

System.out.println("2. Get the word ");

System.out.println("3. Exit this Program ");

System.out.print("\nPlease press Enter afer each response");

System.out.println("\nEnter your choose please: ");

choice = kbd.nextInt();

kbd.nextLine();

if (choice == 1){// if 1 is select

}// close while loop

}

if (choice == 2){// if 2 is select go to find

int randomIndex = Math.abs(((Iterator<String>) arlist).next()getWord.length());

if (choice == 3){

System.out.printf("Good bye");

}// close the choice == 3

// print out all elements of array list

for (WordClass1 idx : arlist){

System.out.printf("Employee here are the list of all Employees Empoyeed");

System.out.printf("Employee Id is %s%n", idx.getWd_id());

System.out.printf("Name is %s %s%n", idx.getWord());

System.out.println("--");

}//close for loop

}

}//close main

}//close public class

my problem lies here in which I'm trying to get the word selected.

int randomIndex = Math.abs(((Iterator<String>) arlist).next()getWord.length());

Any help would be great.

PS this code is for a hangman game.

nomad

[5912 byte] By [Nomada] at [2007-11-27 9:54:39]
# 1

first, what is

int randomIndex = Math.abs(((Iterator<String>) arlist).next()getWord.length());

supposed to do. This will not compile as written. I am going to take a guess at what you are trying...

WordClass1 wc1 = ((Iterator< WordClass1 >) arlist).next() ;

String word = wc1.getWord() ;

int randomIndex = word.length() ;

as a one liner it would look like:

int randomIndex = ((Iterator<WordClass1>) arlist).next().getWord().length() ;

kris.richardsa at 2007-7-13 0:24:35 > top of Java-index,Java Essentials,New To Java...
# 2
Thanks for the help, but when I add your code I get this error nowException in thread "main" java.lang.ClassCastException: java.util.ArrayList at hangman.HangmanWords.main(HangmanWords.java:117)any ideal how to fix this or suggestions.nomad
Nomada at 2007-7-13 0:24:35 > top of Java-index,Java Essentials,New To Java...
# 3

Maybe you could try using Math.random(), which returns a random value from 0.0 to 0.1.

So to get the arlist index, you can multiply arlist.size() by Math.random() to get the random index. But because the index value could be a decimal number, you'd have to round the number.

Perhaps try this:

int randomIndex = Math.round(Math.random() * arlist.size());

Hope that helps!

Anna

annachua at 2007-7-13 0:24:35 > top of Java-index,Java Essentials,New To Java...
# 4

> Perhaps try this: > int randomIndex = Math.round(Math.random() *

> arlist.size());

Random class is much better for this than Math.random.

You tell me which looks better. This:

int randomIndex = Math.round(Math.random() * arlist.size());

or this:

int randomIndex = rand.nextInt(wordList.size());

Message was edited by:

petes1234

petes1234a at 2007-7-13 0:24:35 > top of Java-index,Java Essentials,New To Java...
# 5

A couple of suggestions:

1) You've got a nice WordClass1. Good to see some OOP going on. Why not do the same for HangmanWords class? Why not change the arraylist from a static variable to an instance variable? You can initialize it in the constructor. You then could create a third class: HangmanDriver that has your main. Think encapsulation.

2) arlist I hate to say it is a terrible name for a variable. It tells you what kind of data structure the data is in, something that could easily change in the future, but doesn't tell anything about what kind of data is contained in it. It would be better to call it something else, perhaps hangmanWords or wordList or somesuch thing.

3) try to encapsulate your user interface (I/O) routines into their own class. If you really get this program going, you're going to want to translate it from console to Swing GUI. Why not build it from the ground up in a way that would make this transition easy.

Here's one way the HangmanWords could look:

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.Random;

/**

* encapsulates my hangman word list

* If I make this class implement the Iterable

* interface, I can loop through it more readily.

* @author petes1234

*

*/

public class HangmanWords implements Iterable<WordClass1>

{

private List<WordClass1> wordList;

private Random rand;

public HangmanWords()

{

wordList = new ArrayList<WordClass1>();

rand = new Random();

}

public boolean add(WordClass1 word)

{

return wordList.add(word);

}

public boolean remove(WordClass1 word)

{

return wordList.remove(word);

}

public WordClass1 get(int i)

{

return wordList.get(i);

}

public WordClass1 getRandom()

{

int randomIndex = rand.nextInt(wordList.size());

return get(randomIndex);

}

// this is all we need to implement the Iterable interface

public Iterator<WordClass1> iterator()

{

return wordList.iterator();

}

}

petes1234a at 2007-7-13 0:24:35 > top of Java-index,Java Essentials,New To Java...
# 6
thanks petes1234 for the advice and incouragement!!!!nomad
Nomada at 2007-7-13 0:24:35 > top of Java-index,Java Essentials,New To Java...
# 7

> thanks petes1234 for the advice and

> incouragement!!!!

No prob. Actually, on second look, I'm not all that crazy about my HangmanWords class. All it is is a subset of ArrayList with the getRandom thrown in. It can be improved for sure. One thing to possibly add is a toString method like so:

@Override

public String toString()

{

StringBuilder sb = new StringBuilder();

for (int i = 0; i < wordList.size(); i++)

{

sb.append(wordList.get(i).toString());

if (i != wordList.size() - 1)

{

sb.append(newLine);

}

}

return sb.toString();

}

Where newLine is a class instance variable String that is set equal to System.getProperty("line.separator").

petes1234a at 2007-7-13 0:24:35 > top of Java-index,Java Essentials,New To Java...