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

