File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Using String() method to initialize variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using String() method to initialize variable" Watch "Using String() method to initialize variable" New topic

Using String() method to initialize variable

Klaas Vredevort

Joined: Dec 27, 2005
Posts: 29
Hey everyone. I'm sure everybody in here is getting sick and tired of me and my Hangman program. It's almost finished to a point that I can start using graphics.

This time, I'm doing things right!

The code in question is posted at

The entire package so far is available for download (in case you want to compile it yourself to see what I'm talking about) at

OK, so here we go:

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 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 - 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: you'll need to open Hangman, HangmanFileReader and HangmanManager (which acts like a driver).


Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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.

[Jess in Action][AskingGoodQuestions]
Kannan Venkateshan

Joined: Nov 19, 2003
Posts: 7

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.

Thanks and regards,

I agree. Here's the link:
subject: Using String() method to initialize variable
It's not a secret anymore!