Randomly choose a word from a text file
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.
This is how her code starts out...
Why have you made all your fields static? You have got far too much code in the main method. Code for reading the file should be written in a separate method.
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:
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.
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.
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).