Jonny
Jonny

Reputation: 33

matching a word with other text

I am making a program which takes users data to sign them up for an account. For one of the parts, I want to make an error message for whenever their password they choose has a word in it similar to their username. example: username = John. password = 5john123. Error! your password cannot include the username. I was looking through past questions and I found an answer that helped me. but only to a point. The code someone suggested only made it so that if the password and username are exactly the same, then it would display an error message. This is what they suggested:

if (Arrays.asList(password.split("[\\s]")).indexOf(name) != -1)  
              System.out.println("Error!  Your password cannot include your username");   
              else            
              System.out.println("valid password");

This above code only works if the password and username are the same. if anything is added to either side it doesn't work.

How do I modify this so that no matter if there are numbers added on either side, it still finds whether or not the username is included in the password? Thank you.

Upvotes: 0

Views: 56

Answers (1)

SMA
SMA

Reputation: 37033

You could do something like:

String userName = "John";
String password = "5john123";
if (password.toLowerCase().contains(userName.toLowerCase())) {
    System.out.println("Error! Your password cannot include your username");
} else {
    System.out.println("valid password");
}

Upvotes: 2

Related Questions