473,770 Members | 1,880 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.Class CastException: java.util.Array List at hangman.Hangman Words.main(Hang manWords.java:1 17)

any help would be great.
nomad
Hangman [/size]
Jul 6 '07 #1
2 1602
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
5208
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. I can not transfer a report to word without losing out on the lay-out (RTF format). I understand that there is no way out ok, mail merge I thought. But here I have the problem that I need to merge multiple tables and that I can just include...
0
2897
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: Sub replaceall() '
2
13533
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 execute a MailMerge to create mailing labels or customized letters. A label name known to MS Word MailMerge mailing label wizard may be used or a template file containing the field names Line1 thru Line5 for each record to be printed. If a...
5
5335
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 of it, but I'm having trouble with two pieces: ListGalleries - I can't find any way to make VB.Net understand this. When I do a mouseover, the error message it gives is, "Interface 'Word.ListGalleries' cannot be indexed because it has no...
1
7652
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 ParseWordDoc(ByVal Filename As String) As String Dim sNewFileName As String Dim WordApp As Word.Application = New Word.Application Dim BaseDoc As Word.Document Dim DestDoc As Word.Document
2
3374
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 certain data to the next new line of a opened word doc. Here is what I tried, but doesnt seem to work: Dim objWord As Object Dim NewRow As Row
1
1604
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. Thanks! Point of Sale Terminal ----------------- 1. Record a Transaction 2. Transaction History
4
12441
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 this: can Access create the document and place it as an OLE object to the relevant table? Any help is greatly appreciated. Ricky
0
9617
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10257
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10037
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2849
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.