• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read in a list of words in a text file and randomly choose a word from it  RSS feed

 
Janny Will
Greenhorn
Posts: 5
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello) I am a newbie to this forum as well as Java... My task is to write a Hangman program that requires exception handling and I am not sure how to begin. Specifically, how do I read in a list of words in a text file and randomly choose a word from it?

This is what I have so far...
 
John Joe
Ranch Hand
Posts: 439
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read this ?
Randomly choose a word from a text file
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you've already gotten ahead of yourself. What you have doesn't compile. It's always best to write the program incrementally and at each step get it to compile and work before adding any more to it. I suggest just reading the file and printing each line. You will probably new to look at the documentation for Scanner.nextLine() and Scanner.hasNextLine() for starters. you should be starting with something as simple as this:
To go on to the next level of design you will probably need to put away your keyboard and get out a pencil and paper. Don't try to design everything at once. Design what you want to do with the lines you are reading. That would be a good place to start.
 
Janny Will
Greenhorn
Posts: 5
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the suggestions! I guess I am just trying to understand my professors example if how she first read the file but it was never explained. Is there a simpler or more beginner way of writing the for loop in her code? Because I can't seen to trace what she was trying to do making it difficult to understand.

This is how her code starts out...
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why have you made all your fields static? You have got far too much code in the main method. Code for reading the file shou‍ld be written in a separate method.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use a for statement: use a while.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a simpler or more beginner way of writing the for loop in her code?

I personally don't like that for loop; as Campbell said, I'd use a while loop:

I'd also change line 17 to:

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Janny Will wrote:my professors example if how she first read the file but it was never explained. ...
This is how her code starts out...

Gah (and gag)!!! My pet peeve.

As others have alluded to, your instructor's code is not really the best way to write Java programs, or any program, for that matter.  It looks like all the code in that example is jammed into the main() method. Not good at all.

Programs should be organized and broken down into smaller, focused tasks. This makes it easier to isolate problems and maintain what's called a "separation of concerns."  The concept of separation of concerns is key to writing good object-oriented programs, when you do get around to doing that. Right now, you're only writing procedural code, which isn't what Java was meant for.  So, you're getting the worst of both worlds: procedural code that you have to force into an object-like container and an Object-Oriented language that's not being used in an object-oriented way.

A more organized way to write that code would have been something like this:

If you read the code in the main method, it's easy to figure out that the readWords() method is taking the name of a file and treating it as a file that contains a list of words. The readWords method will then read in the words from the file and return those words as a List<String>.  Then, you create an instance of the Hangman game on line 5 and give it the list of words that it can randomly pick from while the game is being played.  Line 6 just starts the game itself.

Even if you don't know what the details are for all these methods calls, you'd still be able to understand the general flow of the program and what's going on, even on just a high level abstract kind of way.

 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So something to consider...you actually have two distinct problems above:

1) how to read in a list of words from a file into a collection (an array, a map...whatever).

2) Assuming I have a collection, how do I select a random element from it?

When you break down the problem like this, it does several things. It suggests some methods you may want to write. It makes one hard problem into two easier problems. It lets you completely separate out resposibilities. If you get #2 to work, it doesn't matter HOW you fill that colleciton...you could read from a file, pull from a database, ask the user to input each item manually...the point being you can change how the one works, and not worry about it affecting the other.  This is a critical lesson to start learning now.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:...the point being you can change how the one works, and not worry about it affecting the other.  This is a critical lesson to start learning now.

And one that, unfortunately, seems to be often overlooked in favor of teaching all the nuts and bolts of syntax first. In my opinion, teaching syntax without the context of good practices like these is a big part of what's wrong in some of these "modern" teaching methods. My recollection may be muddied and biased now that I've been out of school for so long but I can still distinctly remember (or conveniently misremember, as the case may be) that we were taught to format/indent code properly and break things down into smaller tasks that were delegated to and organized in their own procedures and functions (I learned Pascal and BASIC concurrently as my first languages).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!