Help with a ramdom word selection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nomad
    Recognized Expert Contributor
    • Mar 2007
    • 664

    Help with a ramdom word selection

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

    here is the code so far.
    Code:
     class WordClass1 {// there is another PersonClass1 project.
    Code:
     
    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
     
    [left][/left]

    my problem lies here in which I'm trying to get the word selected.
    Code:
    int randomIndex = ((Iterator<WordClass1>) arlist).next().getWord().length();


    I get this error
    Exception in thread "main" java.lang.Class CastException: java.util.Array List at hangman.Hangman Words.main(Hang manWords.java:1 17)

    any help would be great.
    nomad
    Hangman [/size]
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    You are trying to cast an arraylist into an Iterator. That won't work.
    To get an Iterator from an arraylist just do
    arlist.iterator ();

    P.S It's always a good idea to split long statements into shorter onse so you can see easily what's going on.

    Comment

    • nomad
      Recognized Expert Contributor
      • Mar 2007
      • 664

      #3
      Originally posted by r035198x
      You are trying to cast an arraylist into an Iterator. That won't work.
      To get an Iterator from an arraylist just do
      arlist.iterator ();

      P.S It's always a good idea to split long statements into shorter onse so you can see easily what's going on.
      thanks for the advice 35198x
      nomad

      Comment

      Working...