简体   繁体   中英

making sure a word is a specific length

I am new here and I am making a program to allow a person to sign up for something. It takes in the username, compares it with others, and if it is unique, allows it. When they enter a password however, I don't know how to make sure the word is more than 8 digits. I need to be able to compare the password with something, and only allow the password if it is more than 8 digits or not. Thanks!

You should accept the password into a String type. Then compare it with other String using String.equals(String otherWord) method and check it's length using String.length() method as shown below :-

Scanner s=new Scanner(System.in);
boolean flag=true;
String pwd="";
while(flag) {
pwd=s.nextLine();
if(pwd.length()>8 && pwd.equals("IntendedWord"))
// set flag=false AND then continue your intended action...
else
System.out.println("Please try again");
}

The method you are looking for is String.length() .

Use something like:

if (password.length() > 8) {
    // password is long enough
}

Try something like this:

 // Previous code gets the value for the field and stores it in String pass

 if ( pass.length() > 8 )
 {
    // it is valid. Hash and save
 }
 else
 {
    // Not valid. Let user know and ask for reentry.
 }

 //etc.

You could probably put this and other checking in a validate function and call it before storage.

Let me know if there is anything else you need.

Also, two things to learn about Stack Overflow as a courtesy. Please search for questions similar to yours before posting. And second, give more information when posting so people don't have to guess at what you want/need.

you could use something like

if(password.length() > 8)
{
//some logic
}

that is, considering that you have your password as a string value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM