Win a copy of Spring in Action (5th edition) this week in the Spring 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Java Guess the password program not working  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make a 'guess the password' program but it is not working and I am not sure as to why. Basically, the password is Apple and the user has to try and guess Apple. If they guess it correctly, the program should say "That is correct' (that is not working) but if they guess it incorrectly the program will provide a hint and keep on giving the user that hint until they guess it correctly. Here's my code: (Help is appreciated, thanks!)


 
Saloon Keeper
Posts: 5146
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you cut and paste your interaction with the program, showing your input and the program's output.

Do you start by entering option '2'?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is "Enter option"? You should explain a little better to the user what is he/she required to do.
 
Dee Vai
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interaction with the program:::

Enter option: 2
Enter a password: Password
That is incorrect! Would you like a hint?
Y
it is a fruit. Guess again!Pass
That is incorrect! Would you like a hint?
Y
it is a fruit. Guess again!Apple
 
Dee Vai
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only put option in there as it is part of a bigger program but option is this specific 'Guess the password' portion.
 
Dee Vai
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main problem I am having is why won't it print 'That is correct'? even if the password is guessed correctly??
 
Carey Brown
Saloon Keeper
Posts: 5146
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mostly works for me. You have a bit of broken logic at line 27. You print out "it is a fruit" inside the if() statement, that is good. However you also print "Guess again!" and get the new password1 value inside the if() statement, that is bad. What if you want to enter a new guess without getting a hint?
 
Carey Brown
Saloon Keeper
Posts: 5146
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your logic to print "That is correct!" only exists outside the loop. You'd also need to print it after the loop exits.
 
Pedro Lima
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Some comments about the original code:
1. It is better to avoid unecessary comments  like "variables". It's ok in the beginning of learning but avoid it as quickly as you can.
2. Remember to close the Scanner! (input.close())
3. Examine the starting and ending points of your loops carefuly.

Now, for the Java pros, I am asking a question: do you have a better code for this one, a cleaner one? I'm asking cause sometimes I don't know if it's good practice to use a control variable for the loop, since it's an additional variable. In this case, it is the boolean 'guessed'.

Thank you.
 
Carey Brown
Saloon Keeper
Posts: 5146
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a number of prompting styles with some pros & cons.
 
Marshal
Posts: 61755
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about stye 3½, while (answerWrong) ...
 
Carey Brown
Saloon Keeper
Posts: 5146
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This also works but still has a duplicate line of code.
 
Campbell Ritchie
Marshal
Posts: 61755
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not what I meant. It is simply the reverse of your No 3.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!