Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Password validation  RSS feed

 
marina Gregory
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the code runs but it doesn't exactly read the new password and output comes as:

incorrect password . Please set a correct password
incorrect password . Please set a correct password
incorrect password . Please set a correct password
The password was Test.Please enter a correct new password
password starts with underscore or any case alphabet.//----------->this is the part that is bugging me

i'm kinda new to programming ...please help
i'm supposed to:
Imagine that the password to sign in to some computer is “John”. Write a program that prompts the user to enter a password to login to this imaginary computer. If the user enters the correct password then notify the user and terminate the program. Should the user get the password incorrect three times in a row then inform the user of the current password and allow the user to set a new password. Check the new password so that it satisfies these modified java identifier rules:
1. The password may have any number of characters between 1 and 20, inclusive.
2. The password may start with an underscore “_” or any letter of the alphabet
3. The password may be any combination of upper and lower case letters.
4. The password consists only of numbers, English alphabet letters, and the underscore character.
 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please explain the algorithm you are using to validate passwords. Why are you recording the password in an object? Why are you using so many == signs with booleans? Never use == true nor == false. They are not only poor style, but also very error‑prone.
Never
if (b == true) ...
but
if (b) ...
Never
if (b == false) ...
but
if (!b) ...

What do those regular expressions mean? What does !valid mean in line 37? What does line 54 mean?

Please indent your code correctly; you also had lines much too long to be legible on a screen. I corrected the worst offenders.
 
marina Gregory
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the welcome and the reply
oh...i never knew that it was error prone,thanks for the advice and making it easier to read.
I have been fiddling with this code for a long time now...like its been 5 days now since i am stuck with this looping business.
well, the valid was what i thought , to make it easier for me to give a true or false result...but yeah...it is kinda useless and i thought of just trying it out.
This is something else i did,got the method or saw i could try and do it from someone who posted a question which was kind of similar but not the same in Big Moose Saloon ...using the call by method i guess...
It runs but im not getting the required output...
incorrect password . Please set a correct password
incorrect password . Please set a correct password
incorrect password . Please set a correct password
incorrect password . Please set a correct password
The password was Test.Please enter a correct new password

and does not print out" valid password"

please help

 
marina Gregory
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should the user get the password incorrect three times in a row then inform the user of the current password and allow the user to set a new password. Check the new password so that it satisfies these modified java identifier rules:
1. The password may have any number of characters between 1 and 20, inclusive.
2. The password may start with an underscore “_” or any letter of the alphabet
3. The password may be any combination of upper and lower case letters.
4. The password consists only of numbers, English alphabet letters, and the underscore character.

sorry ,the end of the do while should be "while(!inputPass.equals(passWord) && count<2); "...not count<=2.
If i enter the correct password "Test" ...it outputs fine, but if the password is incorrect i get 2 more attempts
it then prompts the user to enter a new password , if the new password satisfies the rules given "valid password" is given the output
 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I trust you are not building a real website with those rules.

I think you need to start by writing down on paper how you intend to verify such a password. Forget the computer, which is hindering you.
 
marina Gregory
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no,its not for a real website... the same way i typed the code...i am really confused... whats wrong with the code...i doubt i am even getting the logic behind loops
 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The format of your password appears to be the same as for a Java® identifier.

You are going to have to divide and conquer. Remember the old saying: United we stand, divided we fall. At the moment it is the difficulties which are united and it is you falling. So start by dividing them up. Create a class which validates passwords. Start with simple input, then get one task and one only and sort that out. Let's have a shot:-Now all you have to do is write the validateLength method. Give it private access. Go through the methods of the Character class and you may find something to your advantage.

Additional: Execute that with java PasswordDemo
Pass a line at a time; if you push enter twice you should get a 0‑length line. Use ctrl‑Z (Windows) or ctrl‑D (Linux/?Mac) to terminate input.
 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These characters are 2019 201c and 201d: \u2019: ’ \u201c: “ \u210d: ”

Damn! I got the 1 and 0 the wrong way round in the code; I shall go back and correct it.
 
marina Gregory
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there.thank you for the help.meant a lot that you were there to help out. finally figured out the problem...Boolean operators,that is what was giving me trouble...the difference between AND and single version AND or OR and single version OR...



hehe...and no i didn't really understand some parts of the code you showed like "new PasswordValidator(inScan.nextLine()).showValidation();" or why you used a private Demo class or why private final String password was used or why "this" was used...i'm really sorry..i'm kinda new to java ....but thank you for the help...will learn to divide and conquer...do you know any good references for me to look into or extra practice problems that i can look at?
 
Campbell Ritchie
Sheriff
Posts: 53769
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the delay in replying.
You need to pass a String to the PasswordValidator constructor. Any String can be passed. In my case however I have taken a nextLine() call. That returns a String. So you can pass it to the constructor.

Then I imputed a method called showValidation. So you can create a PasswordValidator object, and directly call a method on that. You keep upgrading the validation until you have it all working.
(PasswordValidator object←keyboard input)→showValidation method.

You might have understood it had I written the same method name in both lines (sorry). I meant that you should start with a little bit of the validation and gradually add to it until it is complete. Then, you can remove the contents of the main method and put something real in.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!