473,503 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with a ramdom word selection

nomad
664 Recognized Expert Contributor
Hello everyone.
I'm trying to write a ramdom word program using a arraylist.

here is the code so far.
Expand|Select|Wrap|Line Numbers
  1.  class WordClass1 {// there is another PersonClass1 project. 
Expand|Select|Wrap|Line Numbers
  1.  
  2. private String wd_id;
  3.  
  4. private String word_name;
  5.  
  6.  
  7.  
  8. public WordClass1(String id) {
  9.  
  10. wd_id = id;
  11.  
  12. }
  13.  
  14. public WordClass1(String id, String Wd) {
  15.  
  16. this.wd_id = id;
  17.  
  18. this.word_name = Wd;
  19.  
  20.  
  21.  
  22. }
  23.  
  24.  
  25.  
  26. // accessors
  27.  
  28. public String getWd_id() {return wd_id;}
  29.  
  30.  
  31.  
  32. public String getWord() {return word_name;}
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. public String toString() {
  41.  
  42. return "(" + wd_id + word_name + ")";
  43.  
  44. }
  45.  
  46.  
  47.  
  48. }//close class Person class
  49.  
  50.  
  51.  
  52. public class HangmanWords {
  53.  
  54. static ArrayList<WordClass1> arlist;
  55.  
  56. static Scanner kbd;
  57.  
  58.  
  59.  
  60. public static WordClass1 makePerson() {
  61.  
  62. WordClass1 temp = null;
  63.  
  64.  
  65.  
  66. // prompt for data
  67.  
  68. String id;
  69.  
  70. String Wd;
  71.  
  72.  
  73.  
  74. System.out.print("Enter ID Number ==>");
  75.  
  76. id = kbd.next();
  77.  
  78.  
  79.  
  80. System.out.print("Enter Last Name ==>");
  81.  
  82. Wd = kbd.next();
  83.  
  84.  
  85.  
  86.  
  87.  
  88. // make an object
  89.  
  90. temp = new WordClass1(id, Wd);
  91.  
  92.  
  93.  
  94. return temp;
  95.  
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102. public static void main(String[] args) {
  103.  
  104. // make array list object
  105.  
  106. List < WordClass1 > arlist = new ArrayList < WordClass1 > ();
  107.  
  108. arlist.add(new WordClass1("A1", "STRING"));
  109.  
  110. arlist.add(new WordClass1("A2", "PERSON"));
  111.  
  112. arlist.add(new WordClass1("B1", "CLASS"));
  113.  
  114. arlist.add(new WordClass1("B2", "JAVA"));
  115.  
  116. System.out.println(arlist);
  117.  
  118.  
  119.  
  120.  
  121.  
  122. // make a scanner
  123.  
  124. kbd = new Scanner(System.in);
  125.  
  126.  
  127.  
  128. int choice;
  129.  
  130. System.out.println("Make a Section: ");
  131.  
  132. System.out.println("1. Enter W ");
  133.  
  134. System.out.println("2. Get the word ");
  135.  
  136. System.out.println("3. Exit this Program ");
  137.  
  138. System.out.print("\nPlease press Enter afer each response");
  139.  
  140. System.out.println("\nEnter your choose please: ");
  141.  
  142. choice = kbd.nextInt();
  143.  
  144. kbd.nextLine();
  145.  
  146. if (choice == 1) { 
  147.  
  148.  
  149.  
  150. // create words 
  151.  
  152. }
  153.  
  154. if (choice == 2) { // if 2 is select go to find
  155.  
  156.  
  157.  
  158. int randomIndex = ((Iterator<WordClass1>) arlist).next().getWord().length();
  159.  
  160.  
  161.  
  162. if (choice == 3) {
  163.  
  164. System.out.printf("Good bye");
  165.  
  166. }// close the choice == 3
  167.  
  168.  
  169.  
  170.  
  171.  
  172. // print out all elements of array list
  173.  
  174. for (WordClass1 idx : arlist) {
  175.  
  176. System.out.printf("Employee here are the list of all Employees Empoyeed");
  177.  
  178. System.out.printf("Employee Id is %s%n", idx.getWd_id());
  179.  
  180. System.out.printf("Name is %s %s%n", idx.getWord());
  181.  
  182. System.out.printf("Name is %s %s%n", randomIndex);
  183.  
  184. System.out.println("--------------------");
  185.  
  186. }//close for loop
  187.  
  188. }
  189.  
  190. }//close main
  191.  
  192. }//close public class
  193.  
  194.  

my problem lies here in which I'm trying to get the word selected.
Expand|Select|Wrap|Line Numbers
  1. 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]
Jul 6 '07 #1
2 1585
r035198x
13,262 MVP
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.
Jul 6 '07 #2
nomad
664 Recognized Expert Contributor
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
Jul 7 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
5168
by: svdh2 | last post by:
I have been looking at a problem the entire week in access, I have still not been able to find a solution. Hope that you could maybe tell where to look Concerns the link between Access and Word....
0
2878
by: David Krmpotic | last post by:
Regards, I need a little explanation in converting word VB macros to charp code! first, I recorded simple macro which replaces every occurence of one word in whole document (is it?) , here:...
2
13481
by: Mikey | last post by:
Sample VB .NET source code to create mailing labels or customized letters using MS Word MailMerge This VB .NET source code will start MS Word and call methods and set properties in MS Word to...
5
5322
by: Mason | last post by:
I'm having some problems converting VBA for Word 2000 to code that VB.Net understands. I recorded a macro in Word to add numbering (a. b. c.) to my paragraphs. I managed to translate quite a bit...
1
7617
by: Adam Faulkner via DotNetMonster.com | last post by:
I had a problem before extracting pages from an existing word document and then inserting the content into a new word document. The following code below works with Microsoft Word 2000 Function...
2
3352
by: BerkshireGuy | last post by:
I want to open a word document from an Access form and transfer data from the Access form to the Word Document. For instance, if a user clicks an option box on my access form, I want to pass...
1
1583
by: alfie27 | last post by:
Hi, Here is what the final output to the screen should look like. Below this is the code I have so far. Can someone please help me as to what i need to do next?? I'm really clueless right now. ...
4
12412
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
0
7192
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7064
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7315
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.