Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Using String() method to initialize variable

Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).


author and iconoclast
Posts: 24203
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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,

Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic