In the code below i can\'t seem to figure out a way to get the code to ignore sp
ID: 3637936 • Letter: I
Question
In the code below i can't seem to figure out a way to get the code to ignore special characters when checking equality between passwords, Ex: csci1302 should equal CSci-1-3-0-2. it should ignore the special characters and case. The equals method is toward the bottom of the code. Thanks.public class MasterPassword2 {
//declare private variables
private String userName;
private String password;
//constructor that accepts two string arguments
public MasterPassword2 (String userName, String password)
{
this.userName = userName;
this.password = password;
}
//copy constructor
public MasterPassword2 (MasterPassword2 copyMasterPassword)
{
this(copyMasterPassword.userName, copyMasterPassword.password);
}
//default constructor
public MasterPassword2 ()
{
userName = "guest";
password = " ";
}
//method to set UserName
public void setUserName (String uName)
{
userName = uName;
}
//method to set PassWord
public void setPassword(String pWord)
{
password = pWord;
}
//returns the username for equals method
public String getUserName()
{
return userName;
}
//returns the password for equals method
public String getPassword()
{
return password;
}
//toString method to return values in Username and Password
public String toString()
{
String logIn = "Username: " + userName + " Password: " + password;
return logIn;
}
//deep copy of MasterPassword Object
public MasterPassword2 copy()
{
MasterPassword2 copyMasterPassword = new MasterPassword2(userName,password);
return copyMasterPassword;
}
//equals method to compare submitted MasterPassword Objects username
//and password with calling objects username and password
public boolean equals(MasterPassword2 mw)
{
if(password.equalsIgnoreCase(mw.getPassword()))
{
System.out.println(" Passwords are equal.");
return true;
}
else
{
System.out.println(" Passwords are not equal.");
return false;
}
}
//valid boolean method to check to see if Password contains user name inside
//if username is found in password return false
public boolean valid(){
{
String lowercasePassword = password.toLowerCase();
String lowercaseUserName = userName.toLowerCase();
if (lowercasePassword.indexOf(lowercaseUserName) >= 0)
{
System.out.println("Invalid Password, can not contain the User Name.");
return false;
}
else
return true;
}
}
}
FILE 2: DEMO FILE
import java.util.Scanner;
public class MasterPasswordDemo2 {
static Scanner kb = new Scanner(System.in);
public static void main(String[] args) {
//Create two MasterPassword Objects
MasterPassword2 guest = new MasterPassword2();
guest.setPassword("csci1302");
MasterPassword2 manager = new MasterPassword2(guest);
manager.setUserName("jsmith");
//get input from manager to set new password
boolean validPw = false;
while (!validPw)
{
System.out.println("Manager enter new password: ");
String pwd = kb.nextLine();
manager.setPassword(pwd);
validPw = manager.valid();
}
//Print out Guest User and Password and Manager User and Password
System.out.println("Guest Info: " + guest);
System.out.println(" Manager Info: " + manager);
//check to see if manager user and password equal guest user and password
manager.equals(guest);
}
}
Explanation / Answer
frst convert both the strings to lower case using string.toLowerCase(); and then compare strings using (str1.equals(str2)