I'm trying to write a ramdom word program using a arraylist.
here is the code so far.
Expand|Select|Wrap|Line Numbers
- class WordClass1 {// there is another PersonClass1 project.
Expand|Select|Wrap|Line Numbers
- 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
- public class HangmanWords {
- static ArrayList<WordClass1> arlist;
- static Scanner kbd;
- public static 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;
- }
- public static void 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 W ");
- 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) {
- // create words
- }
- if (choice == 2) { // if 2 is select go to find
- int randomIndex = ((Iterator<WordClass1>) 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.printf("Name is %s %s%n", randomIndex);
- 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.
Expand|Select|Wrap|Line Numbers
- int randomIndex = ((Iterator<WordClass1>) arlist).next().getWord().length();
I get this error
Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList at hangman.HangmanWords.main(HangmanWords.java:117)
any help would be great.
nomad
Hangman [/size]