This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
The program is a normal (so far!) IO-based Hangman program. We all know how that works. If a word is "hangman", the computer gives back "-------" and we have to guess the letters in the word. If we choose "a", the following is returned: "-a---a-" and if we choose "h": "ha---a-" until we get the word.
OK - so my program works like this: I've got three text files, 1.txt, 2.txt and 3.txt with different categories of words. The user selects a category and the word jumps up. (Or, well ----------!). If the user gets the word right, his/her score is added to, and this score is saved in a textfile, hangmanHighScore.txt, given that the user's score is higher than the previous high score.
If you check out the http://pastebin.com/648264 address quickly, look at line 17. It's basically the variable asking for the user's name and it is imported from HangmanFileReader, another .java file that I wrote (available at http://pastebin.com/648296 - the line that asks for the name is 32).
OK, so what now? Well, it reads the name, fine. But then, when you finish the word correctly, it asks you whether or not you want to play again. This poses two problems:
a) It doesn't write the new high score to the text file (the text file currently contains only one thing, the number <0>.
b) If you elect to play again, it again asks for your name. This isn't what I want. I want it to use the same name and continue with the score. It doesn't. It starts over completely.
What I've tried to do, as you can see in line 17 of the first page - is to read the name only once. The method nameInput reads the name from HangmanFileReader and returns playerName. playerName is then supposed to be taken from the method, but it says that the IOException needs to be thrown first.
Right: so I want your help with two things:
a) keeping track of the user's score and writing the high score to the text file.
b) using the player's name - asking for it only once.
Any help? Guys if I'm talking so that you can't understand, feel free to talk to me. I'll be in #Java on QuakeNet under the name georgestobbart for the next hour or so, so feel free to pop in. And again, if you want to compile the program yourself: www.geocities.com/kitaarmykruis/Hangman.zip you'll need to open Hangman, HangmanFileReader and HangmanManager (which acts like a driver).
I deleted your other copy of this post -- not getting a reply in 24 hours isn't an excuse for cross-posting.
It's likely you haven't gotten a reply for two reasons: first, this post is very long, and seems to assume some background of the reader. I'm the moderator of this forum, and I don't remember anything about past threads on this topic.
Second, your link to the actual code doesn't work -- try it. Your second link to the other class does work. Rather than linking to complete files, it's generally better to extract the smallest possible chunk of code that's relevant to a problem, and post that. If you're using good programming styl;e and writing short methods no longer than 6-10 lines of code, then posting one method is often enough to provide the relevant information. Somehow I suspect that's not the case here, unfortunately.
But without having the requisite background, and not being able to see the code, I can answer your second question in generalities: The code that asks for the user's name needs to be moved outside of the "play again?" loop. The variable that holds the name needs to be declared outside of the loop for this to be possible.
Without seeing the code, I don't know why the score isn't written to the file.
I did not get an oppertunity to check your code, however, answering your query in general, i feel that using the flush() method of Java I/O should solve your problem of writting the contents to the text file. Normally what ever we intend to write to the file is stored in a buffer and until we use the flush() method we will not be able to (flush) write any buffered chars to the underlying stream.
check your code if you are flushing the contents properly to be written.