I am trying to insert a count method, where if the user input a password that doesn't match the required criteria in 3 attempts, the system will exit. I have tried to put a count function, where each time the password is incorrect 1 is added to the count, and when count == 3, system.exit. Can I get some advice why this is not working for me. The password conditions are, at least 10 characters, at least 1 upper case, either 2 or 3 numbers and only 1 special Character.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;
public class Test2 {
public static void main(String[] args) {
int count = 0;
System.out.println("Welcome to Humber College");
System.out.println(" ");
System.out.println(" ");
Scanner in = new Scanner(System.in);
System.out.print("Please enter a given password : ");
String password = in.nextLine();
List<String> errorList = new ArrayList<String>();
while (!isValid(password, errorList)) {
System.out.println("The password entered here is invalid");
count = count ;
if (count == 3)
{
System.exit(count);
}
for (String error : errorList) {
System.out.println(error);
}
System.out.print("Please enter a given password : ");
password = in.nextLine();
}
System.out.println("your password is: " password);
}
public static boolean isValid(String password, List<String> errorList) {
Pattern specailCharPatten = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
Pattern UpperCasePatten = Pattern.compile("[A-Z ]");
Pattern digitCasePatten = Pattern.compile("[0-9 ]");
errorList.clear();
boolean flag=true;
if (password.length() < 11) {
errorList.add("Password should not be less than 10 characters");
System.out.println(" ");
flag=false;
}
if (!UpperCasePatten.matcher(password).find()) {
errorList.add("Password must have atleast one uppercase character");
System.out.println(" ");
flag=false;
}
if (!digitCasePatten.matcher(password).find()) {
errorList.add("Password should contain 2 or 3 numbers");
System.out.println(" ");
flag=false;
}
if (!specailCharPatten.matcher(password).find()) {
errorList.add("Password must have 1 speacial character");
flag=false;
}
return flag;
}
}
CodePudding user response:
It's not a right way to increment variable.
You can follow the following ways to increment or decrement variables: Let's say you have an integer count.
First Way:
- count ;
Second Way
- count = count 1;
Third Way
- count = 1;
CodePudding user response:
count
is java for 'increment count
, and then this expression resolves to the value of count
before it incremented'.
So, when you write:
count = count ;
and, let's say count is 5, then this code first increments count
(it is now 6), then the expression resolves as '5', and then you assign that to be the new value of count
. So, now, count is still 5.
The right way to increment is just count
. not count = count
. If you insist on using =
, you'd write count = count 1
, or count = 1
- it's all identical.
CodePudding user response:
The reason is that your counter is not updated and stays at 0
so that if
never triggers.
Just use count ;
instead of count = count ;
in your while loop and it will work like you expect it to.
CodePudding user response:
You can achieve that by throwing an Exception
if the number of attempts was exceeded.
Another possibly is to return from the method (you can use the return
clause with a void
method like that return;
).
int limit = 3; // could be method parameter
int attempts = 0;
while (!isValid(password, errorList)) {
// your logic
attempts ; // incremeting the number of attempts
if (attempts == limit) {
System.out.println("The maximum number of attempts exceeded");
throw new RuntimeException();
}
}