Problem Description:
Some Websites impose certain rules for passwords. Write a method that checks whether a string is a valid password. Suppose the password rule is as follows:
At the moment there's a bug with punctuation in passwords. If you have any strange punctuation, change the password using the account management tool on the forums (it's in the top right corner of all leagueoflegends.com pages). Another possibility is that you have a US account but you're trying to log in to the European client. Organisations utilising the Tender Hub may elect to issue Tenders or other opportunities to potential suppliers by invitation. If you have been invited, these invitation only opportunities will appear on your homepage once you have logged onto the Portal. Access to these opportunities will only be available to invited organisations.
- A password must have at least eight characters.
- A password consists of only letters and digits.
- A password must contain at least two digits.
Write a program that prompts the user to enter a password and displays 'valid password' if the rule is followed or 'invalid password' otherwise.
This is what I have so far:
When I run the program it only checks for the length of the password, I cannot figure out how to make sure it is checking for both letters and digits, and to have at least two digits in the password.
dur8,7311414 gold badges4646 silver badges7272 bronze badges
hanyouchrishanyouchris
6 Answers
You almost got it. There are some errors though:
- you're not iterating over all the chars of the password (
i < password.length() - 1
is wrong) - you start with a digit count of 1 instead of 0
- you make the check that the count of digits is at least 2 as soon as you meet the first digit, instead of checking it after you have scanned all the characters
558k6363 gold badges922922 silver badges10361036 bronze badges
Suppose a valid password has:
- 8 or more characters, but not more than 16 characters
- one or more uppercase characters
- one or more lowercase characters
- one or more digits
- one or more special characters (like $, @, or !)
Code:
Jan Doggen5,3941313 gold badges4747 silver badges9393 bronze badges
Abdul Hanan KhanAbdul Hanan Khan
As previously answered, you should chek all the password characters first. Count your digits and finally check if count is smaller than 2. Here is the referring code.
istovatisistovatis
JAMLJAML
package Method;
/*2. Write a Java method to check whether a string is a valid password.Password rules:A password must have at least ten characters.A password consists of only letters and digits.A password must contain at least two digits.
Expected Output:
- A password must have at least eight characters.
- A password consists of only letters and digits.
- A password must contain at least two digits
Input a password (You are agreeing to the above Terms and Conditions.): abcd1234
Password is valid: abcd1234*/
public class CheckPassword {
}
symun moyeensymun moyeen
dur8,7311414 gold badges4646 silver badges7272 bronze badges
Parag SatavParag Satav