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?
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.
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.
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.
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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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).