I want to write a simple java program to check whether keycode matches a set of conditions.
This is what I have so far:
Scanner keycode = new Scanner(System.in);
System.out.println("Input keycode");
String key1 = keycode.nextLine();
do {
if (key1.length() < 6 || key1.length() > 8) {
System.out.println("must be at least 6 letter and max 8 letter");
return;
}
else {
boolean upper = false;
boolean lower = false;
boolean number = false;
for (char c : key1.toCharArray()) {
if (Character.isUpperCase(c)) {
upper = true;
} else if (Character.isLowerCase(c)) {
lower = true;
} else if (Character.isDigit(c)) {
number = true;
}
}
if (!upper) {
System.out.println("must contain at least one uppercase character");
return;
} else if (!lower) {
System.out.println("must contain at least one lowercase character");
return;
} else if (!number) {
System.out.println("must contain at least one number");
return;
} else {
return;
}
}
} while (true);
System.out.println("Input keycode again");
String key2 = keycode.nextLine();
if (key1.equals(key2)) {
System.out.println("keycode matched");
} else {
System.out.println("keycode dont match");
}
The user is prompted to input a keycode.
The program first checks if it is more than 6 characters and less than 8 characters.
It then checks if it contains a lowercase, uppercase and a number.
I want it to allow user to input the password again if he were to make any mistake rather than doing all over again.
If successful, it will ask the user to input keycode again. If the two keycodes don’t match,
the user is allowed to try again. After 3 unsuccessful tries, the system will reply with ‘keycode mismatch’.
The part I need assistance in is allowing user to input password if it doesn’t fit the requirement and password mismatch. When I enter a password less than 6 characters, I get the following output:
must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
must be at least 6 letter and max 8 letter
Use a
booleanvariable sayredo. And at every place you have used return, replace it withredo = true. And your while condition should bewhile(redo), which will run untilredo is true, that it, user hasn’t entered correct number.Also, move your
inputreading line: –Inside your do-while loop. And also ,reset your
redovariable tofalsein the loop, so that it doesn’t remaintrueforever.