For starters, i am quite new to java, so im not sure if there is a simpler way to do this. I am currently creating a password checker. I have the majority of the code running smoothly, except for my Special Character Check.
My code checks work for length, and whether or not there is a number, however it does not for my special character check. I currently have the if statement set up to where if the one of the characters in the string is not a letter, digit, or space, to have specialCharCheck = true. else, keep it as false.
import java.util.Scanner;
public class Project5_Part1 {
public static void main(String[] args) {
String pass;
String confirm;
boolean parameters;
Scanner in = new Scanner(System.in);
System.out.print("Please enter password : ");
pass = in.nextLine();
System.out.print("Please re-enter the password to confirm : ");
confirm = in.nextLine();
parameters = isValid(pass);
while (!pass.equals(confirm) || (!parameters))
{
System.out.println("The password is invalid");
System.out.print("Please enter the password again : ");
pass = in.nextLine();
System.out.print("Please re-enter the password to confirm : ");
confirm = in.nextLine();
parameters = isValid(pass);
}
if (isValid(pass))
{
System.out.println("The password is valid");
}
}
public static boolean isValid(String pass) {
boolean numberCheck = false;
boolean specialCharCheck = false;
if (pass.length() < 8) {
return false;
} else {
for (int i = 0; i < pass.length(); i++)
{
if(Character.isDigit(pass.charAt(i)))
{
numberCheck = true;
}
if (!Character.isLetterOrDigit(i) && !Character.isSpaceChar(i))
{
specialCharCheck = true;
//System.out.println("Test");
}
else
{
specialCharCheck = false;
}
}
return (numberCheck && specialCharCheck);
}
}
}
am i missing a simple error? I know there is another way to do this, but i felt it looked more efficient to do it this way. And the logic made sense to me.
Aucun commentaire:
Enregistrer un commentaire