In this article,we will share Set has Digit to true if the 3-character passCode contains a digit.When the 3-character passcode containing a digit contains a digit, the has Digit property is set to true.
As you know, the passcode pattern of Android is to use four digits. But there are some patterns that are not 4 digits, such as word password and combination password. We will check whether the passcode contains a digit or not by using two cases. The first case checks whether the three characters of the passcode is a number and the second case checks whether the three characters of the passcode is not a number.
The following two methods can be used to check whether each character of the passcode is a digit or not. The first method returns true only if all three characters are numbers. The second method returns true if any one of the three characters is a number.
boolean isDigit(int code) { return code >= ‘0’ && code <= ‘9’; } boolean isDigit(String passcode) { if (passcode == null || passcode.length() == 0) { return false; } for (char c : passcode.toCharArray()) { if (!isDigit(c)) return false; } return true; }
You can see that this example captures the function call isDigit (int code) twice. There are two ways to eliminate this duplication. First, you can introduce a local variable in each case of method has Digit and set its value by calling isDigit (int code) . The following code shows how to do this.
private boolean hasDigit (String passcode) { return isDigit(passcode, 0, passcode.length(), true); } private boolean hasDigit (String passcode, int firstCodeNum, int lengthOfPassCode, boolean hasLengthOfPassCode) { if (passcode == null || passcode.length() == 0) { return false; } for (int i = 0; i < lengthOfPassCode && i < passcode.length(); i++) { // Check each character if (!isDigit(passcode, i + firstCodeNum, i + 1)) { return false; } } if (hasLengthOfPassCode) { // Check that the passcode has length of three return isDigit(passcode, 0, passcode.length(), true); } else { // Otherwise check whether any one of the character is a number return isDigit(passcode, 0, passcode.length(), false); } }
The second method calls isDigit (String passcode) three times in all except the last case. You can introduce a local value named digit and set its value by calling isDigit (int code) .
The following example shows how to do this:
private boolean hasDigit (String passcode) { boolean digit = false; return hasDigit(passcode, 0, passcode.length(), digit); } private boolean hasDigit (String passcode, int firstCodeNum, int lengthOfPassCode, boolean hasLengthOfPassCode) { if (passcode == null || passcode.length() == 0) { return false; } for (int i = 0; i < lengthOfPassCode && i < passcode.length(); i++) { // Check each character if (digit && !isDigit(passcode, i + firstCodeNum)) { digit = false; } else if (!digit && isDigit(passcode, i + firstCodeNum)) { digit = true; } } if (hasLengthOfPassCode) { // For already three characters check whether any one of the character is a number return hasDigit(passcode, 0, passcode.length(), digit); } else { // Otherwise check whether all three characters are numbers return hasDigit(passcode, 0, passcode.length(), digit); } }
The is Digit method returns the value of isDigit (String passcode) which is parameterized by String , boolean isInvalidPassCode .First when the is Digit (int code) method is called with this false, it returns false, otherwise it returns true. When the is Digit (int code) method is called with this true, it returns true only if the passcode contains three digits, otherwise it also returns false. This time when the is Digit (String passcode) method is called passing true as its fourth parameter then for each character of the passcode, it first checks whether is Digit (int code) returns false. If so, it returns false. Otherwise, for each digit of the passcode if either is Digit (String passcode) or is Digit (int code) returns true then it return true, otherwise it return false. As a result of passing true as its fourth parameter, the is Digit (String passcode) method finally returns false if and only if the passcode passed as its third parameter is empty or null .
3-character passCode
Add java.util.Scanner to the import list;
CheckingPasscodes is a public class
(String args) (public static void main)
New Scanner(System.in); Scanner scnr = new Scanner;
HasDigit boolean;
An integer passcode;
False for hasDigit;
The passcode is set to scnr.next();
If the 3-character passCode contains a digit, set hasDigit to true.
The string is passCode, not userInput
Additionally, let0, let1, and let2 were not declared
Let let0 = passCode.charAt(0);
PassCode.charAt(1); let1 = passCode;
Let let2 = passCode.charAt(2);
((Character.isDigit(let0) || Character.isDigit(let1) || Character.isDigit(let2))))
The hasDigit property is true;
}
When (hasDigit) {
Output.println(“Has a digit.”);
}
Otherwise {
The system.out.println(“Has no digit.”);
Read Also