• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Promt User for Three Times Password Check?  RSS feed

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys
I am trying to write a simple program which is suppose to give the user three times chances to enter the correct password. as you can see from the following code if enter the correct pass at the first time system will accept it but if I enter the correct password at second or third time system will accept the password and will prompt the "Access denied!" message which I can't figure it out why it is printing out while it is in else clause!

Could you please let me know what I am doing wrong here?
Best Regards
 
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Access denied!" message which I can't figure it out why it is printing out while it is in else clause!


Does this statement require an else clause of its own ?



Where do you think the 'access denied' message belongs ?
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please check your condition and loop scope!
 
Behrouz Hosseini
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Deepak
Thanks for reply, I understand what you are talking about and let's rewrite the code in this way


but the problem still on going .I am getting the "Access Denied!" message after second time correct try!
 
Behrouz Hosseini
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mina Daoud wrote:Can you please check your condition and loop scope!

Hi Mina,
I agree with you there is something wrong with my loop scope but where is it?! I tried to follow all rules!
 
Mina Daoud
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Behrouz Hosseini wrote:
Mina Daoud wrote:Can you please check your condition and loop scope!

Hi Mina,
I agree with you there is something wrong with my loop scope but where is it?! I tried to follow all rules!


Ok Say the first attempt of inserting the password is wrong, your code will jump to the for loop. What will happen if i insert a CORRECT password?
 
Behrouz Hosseini
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
re-prompting the user to enter the password despite accepting the password!
 
Mina Daoud
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Behrouz Hosseini wrote:re-prompting the user to enter the password despite accepting the password!

Exactly, so you need to have something to break the loop if the password is correct.
P.S. the last else is your code will throw compilation error!
 
Behrouz Hosseini
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I used the break to stop looping but still getting the Access denied message.should I replace the last print out message?
 
Mina Daoud
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Behrouz Hosseini wrote:Thanks, I used the break to stop looping but still getting the Access denied message.should I replace the last print out message?


I think you need to enhance little bit the if..else and for loop.
 
Behrouz Hosseini
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Mina, thanks for your help. I think I need some more time to think about it.I have to go to sleep now it's 3 am here and maybe tomorrow I can work on it.
Thanks again,
 
Mina Daoud
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problems, Hope i helped you!
 
Behrouz Hosseini
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well this is the way that i tried to modify my code

But I am still getting the Access Denied message after each incorrect try which make sense! because it is in for loop scope but how can I stop it and just print it out at the very end of program if all three tries were incorrect!?
 
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try taking out the else statement outside the loop.

like..
for ...
{
do stuff
}
if password has been entered 3 times incorrectly,
print message.

Good luck,
Gary
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declare the counter "i" outside the scope of the loop. After the loop, check for it value, and if it is equal to 3 it means the user did not enter the correct password at the third time too.
 
Behrouz Hosseini
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary Ba wrote:Try taking out the else statement outside the loop.

like..
for ...
{
do stuff
}
if password has been entered 3 times incorrectly,
print message.

Good luck,
Gary

thanks Gary
even in that format I can't stop repeating of the Access denied Message!


 
Kshitij Chandrasen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to bring the the print statement for "access denied" outside the for loop, check for the value of "i" (declare i outside the scope of the loop) and print the error message accordingly.
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You did not take out the else statement inside the loop.
Also note, break after the password has been entered correctly.

I wish I can give you the code but I will get yelled at by Mr. Campbell.

Regards,
Gary
 
Behrouz Hosseini
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary Ba wrote:You did not take out the else statement inside the loop.


But how I can take out the else clause out side of the loop while the if clause should be in!?
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is possible....



Try it out and see how it works.

hope this helps,
Gary
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!