Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hangman game question jTextField string check character  RSS feed

 
Thomas Walis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm Trying to write a Simple Hangman game, so far i have managed to get the word length and the word chosen randomly. However i can't find the way to implement user input. The idea is that user inputs a letter in to JTextField and the XXX is substituted if the letter is guessed correctly. I would appreciate if someone can help me with sorting this out, please have a look at the code i have so far, also pleas note this is done in NetBeans.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Thomas.

Seems like there must be rather more to this program than you've posted. I'm guessing that, after the user enters their guess, they click a button called btGuess, right? Have you tried using the NetBeans debugger to see what happens in your routine? I'd put a breakpoint at Line 18, and see if the letter is being retrieved from uInput correctly. Start with that and let us know what you see.
 
Thomas Walis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
this is whole code so far as im stuck at the moment
im showing static String [] words = { "dog", "java" }; in the label called Letter0
user should input a letter in to text field called uInput.
From running the program i can see that words are picked at random as i can see a number of X equivalent to the words in the string. Currently when user inputs a letter noting happens as i have no clue how to get those strings checked against user input and place input character in to a correct place instead of X
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Walis wrote:this is whole code so far

It can't be the whole code. It has no "main" method. What creates the uInput text field? What creates the Letter0 label?

If you post something that others can compile and run, maybe we can help you.
 
Thomas Walis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GUI is generated in NetBeans, i dont think you wish to see all the code generated by it
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go ahead and post it. It's not all that much, and I'm used to dealing with NetBeans's GUI code . Meanwhile, have you tried the debugger yet?
 
Thomas Walis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have changed the code since last post but there you go all NetBeans code
Hope you guys will be able to help, i think i have all the code i need now but the problem is that it doesent work when the user inputs a character it should check if that character appears in the string showed as XXXX

the idea of the program is
Step 1 Read the length of string such as “dog” string is chosen as random and display corresponding XXX in the jLabel (done)
Step 2 User enters single character in to a jTextField, program reads user input.
Step 3 program checks the user input and looks for the equivalent character in the string (i have problem with this )
Step 4 program displays inputted character (only if guessed correctly) in the jLabel (i have problem with this )
Step 5 if character is not correct add one to label called wrongGuess and start to show picture of hangman
Step 6 if all inputted characters matches the string display “YOU HAVE WON”
Step 7 if all inputted characters don’t matches the string display “YOU HAVE LOST ”

 
Norm Radder
Ranch Foreman
Posts: 2216
28
 
Thomas Walis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes is it a crime to look for help?
 
Campbell Ritchie
Sheriff
Posts: 55338
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, but it is discourteous to ask in several places without people being aware of that.
 
Norm Radder
Ranch Foreman
Posts: 2216
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolutely no, its not a crime. The problem is that helpers on the other sites could be wasting their time trying to help you when you've gotten help somewhere else. It is more friendly to provide links to all the sites where you've asked a question so everyone can see what has already been posted and not duplicate an answer already provided.
 
Thomas Walis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh sorry about this , I wasn't aware, how about going back to the topic and help me sort this out?
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this probably isn't going to solve all your problems, but, at Line 42, where you have this:



You really want something more like this:



Typing a character into a JTextField doesn't generate an ActionEvent. It generates a KeyEvent.
 
Norm Radder
Ranch Foreman
Posts: 2216
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typing a character into a JTextField doesn't generate an ActionEvent.

But hitting Enter does.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
Typing a character into a JTextField doesn't generate an ActionEvent.

But hitting Enter does.


I don't read his steps to say that's what he says he wants:

Thomas Walis wrote:the idea of the program is
Step 1 Read the length of string such as “dog” string is chosen as random and display corresponding XXX in the jLabel (done)
Step 2 User enters single character in to a jTextField, program reads user input.
Step 3 program checks the user input and looks for the equivalent character in the string (i have problem with this )
Step 4 program displays inputted character (only if guessed correctly) in the jLabel (i have problem with this )
Step 5 if character is not correct add one to label called wrongGuess and start to show picture of hangman
Step 6 if all inputted characters matches the string display “YOU HAVE WON”
Step 7 if all inputted characters don’t matches the string display “YOU HAVE LOST ”


Part of my problem in helping him is that I think he could do a lot of his own troubleshooting, and tell us more about what his program is doing, if he'd use the debugger. His JTextField can take more than one character, so I don't know yet if he means Step 2 to be "type a character and watch the program respond to a KeyEvent," or to be "type a character, then hit Enter, then watch the program respond to an ActionEvent."
 
Norm Radder
Ranch Foreman
Posts: 2216
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with grabbing the input immediately doesn't allow for changes before deciding this is what is to be entered. The Enter key says this is it.

My efforts on the other site was to get the OP to define what the program was to do at a much more detailed level. The list he posted was a first pass. I'd found 3 other sites he'd posted on. Perhaps there are more and one of them has given him the code he wants.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:The problem with grabbing the input immediately doesn't allow for changes before deciding this is what is to be entered. The Enter key says this is it.


If that's how he wants to do it. A live keyboard works too, and his spec seems to suggest that's what he wants.

More to the point (and it sounds like you've had the same frustration), he's not being clear about what his program is supposed to do.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!