I am suppose to write a method that checks whether a string is a valid password. The password rules are:
1.) A password must have atleast 8 characters
2.) A password consists of only letters and digits
3.) A password must containt 2 digits

I need to write a test program that will ask the user to enter password and Display "Valid Password" if all the rules are followed othewise display "Invalid Password."

Can anyone help me get started...because i have no idea..
Any help is greatly appreciated.

user regular expressions
Check out the pattern class and the Matcher class in the java api

public boolean checkPassword(String password) {
  // your logic here
}
public boolean checkPassword(String password) {
  // your logic here
}

You're a real asshole lol

I am suppose to write a method that checks whether a string is a valid password. The password rules are:
1.) A password must have atleast 8 characters
2.) A password consists of only letters and digits
3.) A password must containt 2 digits

I need to write a test program that will ask the user to enter password and Display "Valid Password" if all the rules are followed othewise display "Invalid Password."

Can anyone help me get started...because i have no idea..
Any help is greatly appreciated.

Write 3 different methods. One to test each of the three rules that you just posted above. For example, method one should check to make sure the password has enough characters. You can use the String class for this; Google for "Java String" and start reading the API. One useful method is the length() method.

For part two you can check out the Character class, especially the isDigit method and the isLetter method. Ditto for part 3.

commented: Nice +20

I'd also recommend using regular expression, but it's a real pain in the neck to wrap your brain around the first few times you try to figure it out.

Since it looks like this is more for school than work (and you're just learning), you might want to just start with real basic code that is easy to read and does it all 'manually' for you (vs using regex). Here's a starting point:

public boolean checkPassword(String password) {
   if(password.length() < 8)
     return false; //length was less than 8 so it's invalid. no need to do further validation

//put basic code here to check to make sure it only contains valid characters. a - z and 0-9 or return false


//put code here to ensure 2 characters are integers or return false


   return true;//if it's made it this far it's valid - return true
}

again, regex (complex) is the way to go if you wanted to figure that one out, but a real easy way is to create a string with valid characters only, and then check each character in the password to see if it exists in the valid characters list. I'm not going to do your homework for you, but here's some pseudo code:

String validChars="abcdefghijklmnopqrstuvwxyz0123456789";


for(int i=0;i<password.length();i++){
//now iterate through each character in the password to see if it's
//in the above list using a for/next loop and password.charAt(i)
}

how do you write the loop that checks to make sure there is atleast 2 digits in the password?

you don't need a loop, you need an if statement, and the correct code was already given above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.