This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin Cookbook and have Ken Kousen on-line!
See this thread for details.
Win a copy of Kotlin Cookbook this week in the Kotlin 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Error check for Hangman

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I am a very new to computer programming and I need help debugging and understanding how to code hangman for a class. So far I only know the basics such as methods, classes, and objects but, I do not know anything about scanners. My program may look weak but it is hard for me. I need help on figuring out how to receive a letter from the user, checking it if it is valid and it exists in the word, if it does then outputs the letters that the user guess correctly and a loop that allows the user to re-enter until the user gets the word right.




 
Sheriff
Posts: 6568
176
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Mark Jinx!

You code is well formatted except for a few places where you have more than one blank line.

You are having a bit of trouble with Java NamingConventions (that's a link).  Specifically, method and variables should start with a lowercase letter.

More to come...
 
Knute Snortum
Sheriff
Posts: 6568
176
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a problem with reading console input (Scanner can be used but also BufferedReader).  What I see is in HangManMethods at lines 117 and 118, you're not taking into account of the fact that indexOf() may return -1 when it doesn't find anything.  -1 isn't a valid index to an array.

Also, I don't think this line is doing what you want:
This will search the guessletter String for 'a', starting at the second letter (indexes are zero-based.)
 
Knute Snortum
Sheriff
Posts: 6568
176
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing: you have a lot of unused variables in your classes.  Remove them.
 
Mark Jinx
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks that really helped. For guessletter.indexOf('a',1), I am trying to find the location of the next 'a' that exists in the word. How would I adjust it to make it not return a -1?
 
Marshal
Posts: 24849
60
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Jinx wrote:Thanks that really helped. For guessletter.indexOf('a',1), I am trying to find the location of the next 'a' that exists in the word. How would I adjust it to make it not return a -1?



Why would you want to do that? Returning -1 tells you that there aren't any more 'a' letters in the word, and I believe you want to do that. Anyway you can't change the behaviour of a method from the standard API, all you can do is to change your code to work with it the way it's written. So I think you need a different question than that one. My question you already have: why don't you want to deal with the possibility of no more 'a'?
 
Mark Jinx
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My word is 'aposematic'. What I am trying to accomplish is that if the user enters 'a' I am trying to find the where both 'a' are located in the string. Then I would store those values and output on the board where both 'a' exists. Example, if the user enters 'a', I am trying to show the user that the 1st and 7th slot of the word has 'a'.
 
Paul Clapham
Marshal
Posts: 24849
60
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. So you're assuming that there are exactly two a's in the word for a start. Here's the code you have so far:



The first line of code says "Give me the location of the first a in the string". Since you know there are exactly two a's, this returns the index of the first a.

Now you want some code which says "Give me the location of the next a after that one". The second line of code doesn't do that. It starts searching at index number 1, whereas you want to start searching at the index which is one greater than the index of the first a. So you need to change it to do the right thing.

Of course, that's all based on your assumption of exactly two a's. There's nothing in the code you have written which guarantees that will be the case, and since the input is coming from the user (a notoriously unreliable source) your code should allow for the possibility of only one a, or none at all, or even three or more a's. That's where you need to take into account the situation where the indexOf() method tells you it didn't find anything.
 
Mark Jinx
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found another alternative for my code. Now I am stuck with the hints, how to repeat/refresh my variables if the user wants to play again and allow the user to only enter in one letter at a time.

 
Mark Jinx
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Knute Snortum
Sheriff
Posts: 6568
176
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some notes on HangManDay5:

* Method names and variable names start with a lowercase letter.

* Don't write if (boolean == true) just write if (boolean)

* Don't write if (boolean == false) just write if (!boolean)

* Don't write if (wrong == true) { ... } else if (wrong == false) { ... } just write if (wrong) { ... } else { ... }

* Don't write methodName (arg), write methodName(arg)
 
Knute Snortum
Sheriff
Posts: 6568
176
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notes on HangManMethods:

You're writing code like this a lot:
guessletter.charAt(0) is used over and over.  Instead, try
Your code is very brittle because it only works for one word.  Generalize your code so that it will work with any word.
 
Mark Jinx
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I be able to check if the letter from the user exists in the word and then output it onto the board?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Marshal
Posts: 67036
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
“Joh Doe,” welcome to the Ranch
Please note this, which is on the contents page for this forum:-

We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.

It doesn't help anybody learn to provide a complete solution, least of all without explanation. I have removed it. The forum records the old text, so it is possible to restore it later.
 
Knute Snortum
Sheriff
Posts: 6568
176
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Jinx wrote:How would I be able to check if the letter from the user exists in the word and then output it onto the board?


Start by "saying" or writing down exactly how you'd solve the problem to a five-year-old.  Then see if you can turn that algorithm into code.

I would think the first thing you'd say is, "Find out if the letter is in the word."  You could do that with one indexOf() call.  It doesn't matter if there are more letters it matches.

Then, "Put all the letters that match the guess into the correct positions for the word to guess."  I'd try this by having a displayGuesses String filled with underlines.  Then use the second form of indexOf to find that places to insert the guessed letter.

That's all I have time for now, but we can "flesh it out" more later.  Try it on your own, though.
 
Popeye has his spinach. I have this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!