• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using String() method to initialize variable

 
Greenhorn
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 http://pastebin.com/648264

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 www.geocities.com/kitaarmykruis/Hangman.zip

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 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).

Thanks!

Klaas
 
author and iconoclast
Posts: 24207
46
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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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,

Kannan
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic