Issue with or operator in Java - || - java

I'm writing a password validation program and having some issues with the or statement that checks for special characters. I'm not sure what is wrong with it, the rest of the program seems to function properly, but entering a password that matches the criteria, for example, P#ndas123$%, just results in an infinite loop.
I believe that the problem is related to my big statement for the !pass.contains("") portion. If that is right, could someone help me understand why the statement is incorrect?
import java.util.Scanner;
public class DylanJacksonProg5 {
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
boolean valid;
valid = true;
do {
//Password rules declared to user
System.out.println("Password Verifier");
System.out.println("");
System.out.println("Enter a password that meets the following rules: ");
System.out.println(" Is at least 8 characters long");
System.out.println(" Contains at least 1 lower letter character");
System.out.println(" Contains at least 1 upper letter character");
System.out.println(" Contains at least 1 numeric digit");
System.out.println(" Contains at least 1 special character of the following set: !##$%^&*");
System.out.println(" Does not contain the word 'and' or the word 'the'");
//Gathering password from user
System.out.println("Enter your password: ");
String pass;
pass = user_input.next();
//Validating password to rules
if (pass.length() < 8) {
int shortlen;
shortlen = 8 - pass.length();
System.out.println("Password is too short. Please add " + shortlen + " characters");
valid = false;
} else
System.out.println("Password length is acceptable.");
//Check for character cases
boolean hasUppercase = !pass.equals(pass.toLowerCase());
boolean hasLowercase = !pass.equals(pass.toUpperCase());
if (!hasUppercase) {
System.out.println("Must have an uppercase Character");
valid = false;
}
if (!hasLowercase) {
System.out.println("Must have a lowercase Character");
valid = false;
}
if (hasUppercase) {
System.out.println("Has an upper case character");
}
if (hasLowercase) {
System.out.println("Has a lowercase Character");
}
//Check for digit
for (int i = 0; i < pass.length(); i++) {
char x = pass.charAt(i);
if (!Character.isDigit(x)) {
valid = false;
}
}
//check for special character
if (!pass.contains("!") || !pass.contains("#") || !pass.contains("#") || !pass.contains("$") ||
!pass.contains("%") || !pass.contains("^") || !pass.contains("&") || !pass.contains("*"))
{
valid = false;
System.out.println("Password requires a special character from the following: !##$%^&*");
}
// check for 'and'
if (pass.contains("and")) {
valid = false;
System.out.println("Cannot contain 'and'");
}
//check for 'the'
if (pass.contains("the")) {
valid = false;
System.out.println("Cannot contain 'the'");
}
System.out.println("");
}
while (!valid);
}
}

I'll tell you one problem straight up front.
Since you set valid to true before the validation loop, and only ever set it to false within the loop, entering an invalid password as your first attempt will result in the loop never exiting.
The setting of valid to true should be the first thing done within the loop.
On top of that, your checks each individually apply to every character in the input string, a situation which is impossible. For example, you check whether the entire string is equal to its lowercased variant, then you check whether it's also equal to its uppercased variant.
That means any string with a letter in it will be deemed invalid since xyZZy cannot be equal to both xyzzy and XYZZY at the same time.
It would be more correct to do something like (pseudo-code, and cut down to a few conditions just to show the method):
do:
hasLowercase = false
hasUpperCase = false
hasNumeric = false
isLongEnough = false
get string from user
if len(string) >= 8:
isLongEnough = true
for each character in string:
if character is lowercase:
hasLowerCase = true
if character is uppercase:
hasUpperCase = true
if character is numeric:
hasNumeric = true
isValid = hasLowerCase && hasUpperCase && hasNumeric && isLongEnough
until isValid
This checks each individual character so that any of them (rather than requiring that all of them) will mark a condition as true.

The OR operators will conflict if you don't have all of the special characters in the password, because in your condition statements, if you have "!" but not "#", it will still result in a false value.
What you can do is check for each special character separately.

Sorry for the lack of explanation, but it is your negation statements in your special character checking block. Usually using !condition works, but it didn't on this online IDE I am using. Not really sure why.
This code will work:
//check for special character
if (pass.contains("!") || pass.contains("#") || pass.contains("#") || pass.contains("$") ||
pass.contains("%") || pass.contains("^") || pass.contains("&") || pass.contains("*"))
{
valid = true;
System.out.println("Password requires a special character from the following: !##$%^&*");
}
else
{
valid = false;
}

There are multiple problems with your code, which are unrelated to each other:
You never set valid to true in the loop, which means that if you enter 1 wrong password, valid will be and always stay false, which means that no matter how many correct passwords you enter afterwards, the loop will repeat infinitely. In other words: the program will only stop if you enter a correct password at the very first attempt.
The big boolean expression is not checking whether the password contains any of the characters, it is checking whether the password contains all characters. Draw the truth table or apply DeMorgan's Laws, and you will see:
!p.c("!") || !p.c("#") || !p.c("#") || !p.c("$") || !p.c("%") || !p.c("^") || !p.c("&") || !p.c("*")
// is the same per DeMorgan's Laws as
!(p.c("!") && p.c("#") && p.c("#") && p.c("$") && p.c("%") && p.c("^") && p.c("&") && p.c("*"))
As soon as you find a single character that is not a digit, you reject the password, IOW all characters must be digits.
It should be obvious that #2 and #3 contradict each other, so it it simply impossible to enter a valid password, so even if you fix #1, it will still not be possible to exit the loop.

There are a 3 issues with your code:
valid must be set to true inside your loop before any checks are done on input:
do {
valid = true;
...
lower & upper case checks should be done differently, for example:
boolean hasUppercase = false;
boolean hasLowercase = false;
for (int i = 0; i < pass.length(); i++){
if (Character.isUpperCase(pass.charAt(i))){
hasUppercase = true;
}
if (Character.isLowerCase(pass.charAt(i))){
hasLowercase = true;
}
}
your checks for special character are incorrect, you could use below instead:
if (!(pass.contains("!") || pass.contains("#") || pass.contains("#") || pass.contains("$") || pass.contains("%") || pass.contains("^") || pass.contains("&") || pass.contains("*"))){
valid = false;
}
EDIT: actually there's another one:
4. Your check for digit also won't work, you could use below instead:
boolean isDigit = false;
for (int i = 0; i < pass.length(); i++) {
char x = pass.charAt(i);
if (Character.isDigit(x)) {
//valid = false;
isDigit = true;
break;
}
}
if (!isDigit)
valid = false;

Here's how OR operator works:
if( condition1 || condition2 || condition3 || condition4)
{
// .....your code;
}
If condition1 is true,then remaining conditions will not be checked and program will move to inside your IF block.However,if Condition1 is false,then condition2 will be checked.If this is true,then remaining conditions will not be checked,otherwise(if it is false) condition3 will be checked.and so on.However as other peoples have already mentioned there is more problem with your code than just with OR operator.
For example,for password=P#ndas123$%,it does not contain "!",so !pass.contain("!") will give true and valid will be set as false thus causing infinite loop.

Related

Java.... Prints 1 error for each character in the input

Im a rookie Java coder, and I am trying to make a very basic username/password program. The username part of it is working fine, but when I get to the password it gives me some weird problems. I figured out that for example when it checks for an Uppercase letter, if it finds one its all good, but if it doesn't, it prints the error message for every single character in the password. It does this with the number check and the length check as well. If any of you guys could explain this to me rather simply since I am still new to java, that would be awesome. Thanks!
do
{
if (count3 >0)
{
System.err.println("- At least 1 Uppercase");
System.err.println("- At least 1 number");
System.err.println("- At least 7 characters long.");
}
regPassword = input.nextLine();
regPasswordLen = regPassword.length();
for(int count = 0; count < regPasswordLen; count++)
{
if(Character.isUpperCase(regPassword.charAt(count)))
regPasswordUppercaseCheck = true;
else
{
System.err.println("Your password did not contain an Uppercase letter");
regPasswordUppercaseCheck = false;
}
if(regPassword.contains("1") || regPassword.contains("2") ||
regPassword.contains("3") || regPassword.contains("4") ||
regPassword.contains("5") || regPassword.contains("6") ||
regPassword.contains("7") || regPassword.contains("8") ||
regPassword.contains("9") || regPassword.contains("0"))
regPasswordNumCheck = true;
else
{
System.err.println("Your password did not contain at least 1 number.");
regPasswordNumCheck = false;
}
if (regPasswordLen >=7)
regPasswordLengthCheck = true;
else
{
System.err.println("Your password did not meet the minimum length requirements.");
regPasswordLengthCheck = false;
}
}
count3++;
}
while(!regPasswordUppercaseCheck || !regPasswordNumCheck || !regPasswordLengthCheck);
System.out.println("test");
You used same variable every time for "if and else" for every different char i.e. regPasswordUppercaseCheck, if every char of your input is in uppercase except the last char, the variable will contain false.
I think you use count3 for making sure that inner code will run single time but if while goes false and count3 condition is remain true then code will stuck in a infinite loop.
Use
while(regPasswordUppercaseCheck && regPasswordNumCheck && regPasswordLengthCheck); for simplicity.
A few things you can change in your program.
do
{
if (count3 >0)
{
System.err.println("- At least 1 Uppercase");
System.err.println("- At least 1 number");
System.err.println("- At least 7 characters long, but no more than 15 characters.");
}
regPassword = input.nextLine();
regPasswordLen = regPassword.length();
// this check only needs to happen once per password, no need to check it in the for loop. You also specified that the length should not exceed 15 characters, so I threw that in as well
if (regPasswordLen < 7 || regPasswordLen > 15)
System.err.println("Your password did not meet the length requirements.");
// by default, we set these flags to false, and only make them true if the requirements are satisfied
regPasswordUppercaseCheck = false;
regPasswordNumCheck = false;
for(int count = 0; count < regPasswordLen; count++)
{
// store the value of regPassword.charAt(count) in a local variable for reusability
char current = regPassword.charAt(count);
if(Character.isUpperCase(current))
regPasswordUppercaseCheck = true;
// checks if the character is a digit or not
if(current >= '0' && current <= '9')
regPasswordNumCheck = true;
}
if (!regPasswordNumCheck)
System.err.println("Your password did not contain at least 1 number.");
if (!regPasswordUppercaseCheck)
System.err.println("Your password did not contain an Uppercase letter");
count3++;
}
while(!regPasswordUppercaseCheck || !regPasswordNumCheck || !regPasswordLengthCheck);
your checking for uppercase is not done right because the loop
for(int count=0;count<regPasswordLength;count++) should not contain the checking if the password contains a number nor the checking if the password is longer than 7 characters so the loop should look like this
for (int count = 0; count < regPasswordLen; count++) {
if (Character.isUpperCase(regPassword.charAt(count)))
{regPasswordUppercaseCheck = true;break;}
}
i use break here to get out of the loop the moment i found that password contains an uppercase after some modifications your code can look like this
do {
if (count3 > 0) {
System.err.println("- At least 1 Uppercase");
System.err.println("- At least 1 number");
System.err
.println("- At least 7 characters long, but no more than 15 characters.");
}
regPassword = input.nextLine();
regPasswordLen = regPassword.length();
for (int count = 0; count < regPasswordLen; count++) {
if (Character.isUpperCase(regPassword.charAt(count)))
{regPasswordUppercaseCheck = true;break;}
}
if(regPasswordUppercaseCheck==false){
System.err
.println("Your password did not contain an Uppercase letter");
regPasswordUppercaseCheck = false;
}
regPasswordNumCheck = regPassword.contains("1") || regPassword.contains("2")
|| regPassword.contains("3")
|| regPassword.contains("4")
|| regPassword.contains("5")
|| regPassword.contains("6")
|| regPassword.contains("7")
|| regPassword.contains("8")
|| regPassword.contains("9")
|| regPassword.contains("0");
if(regPasswordNumCheck==false) {
System.err
.println("Your password did not contain at least 1 number.");
regPasswordNumCheck = false;
}
if (regPasswordLen >= 7)
regPasswordLengthCheck = true;
else {
System.err
.println("Your password did not meet the minimum length requirements.");
regPasswordLengthCheck = false;
}
count3++;
} while (!regPasswordUppercaseCheck || !regPasswordNumCheck
|| !regPasswordLengthCheck);
System.out.println("test");

Check for a specific set of characters(of a password) in a string

I have to write a program which checks a password.
If the password entered by the user is 'bolt', it will display 'The password is valid'
Otherwise it will display 'The password is invalid'.
The program is working only for the if part but not for the else.
That is when I input bolt, it displays the correct message.
But when I enter something other than bolt, it does not display 'The password is invalid'.
I have been told to test for all four characters that is to use char.At.
import java.util.Scanner;
public class MyClass {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter your 4-character password:");
String password = input.next();
for (int i = 0; i < password.length(); i++) {
if (password.charAt(i) == 'B' || password.charAt(i) == 'b')
if (password.charAt(i + 1) == 'O'
|| password.charAt(i + 1) == 'o')
if (password.charAt(i + 2) == 'L'
|| password.charAt(i + 2) == 'l')
if (password.charAt(i + 3) == 'T'
|| password.charAt(i + 3) == 't') {
System.out.println("The password is valid");
}
else {
System.out.println("The password is invalid!");
}
}
}
It would be more readable to check using equalsIgnoreCase():
String password= input.next();
if(password.equalsIgnoreCase("bolt"){
System.out.println("The password is valid");
}
else{
System.out.println("The password is invalid!");
}
Why don't you just check for String equality directly?
String password= input.next();
if("bolt".equals(password))) {
System.out.println("Valid password");
} else {
System.out.println("InValid password");
}
Or use equalsIgnoreCase() if you'd also consider BOLT or Bolt valid.
If you need to implement this without equals you could use something like this:
if (password != null &&
password.length() == 4 &&
(password.charAt(0) == 'B' || password.charAt(0) == 'b') &&
...) {
System.out.println("Valid password");
} else {
System.out.println("InValid password");
}
For your updated question:
String password = input.next().toLowerCase();
String correctPassword = "bolt";
if (password.length() != correctPassword.length()) {
System.out.println("Not valid");
return;
}
for (int i = 0; i < correctPassword.length(); i++) {
if (password.charAt(i) != correctPassword.charAt(i)) {
System.out.println("Not valid");
return;
}
}
System.out.println("Valid");
In your code there are two problems,
1. Your for loop doesn't serve any purpose because you have checked the whole password in the first iteration itself. Your second iteration would cause IndexOutOfBoundsException.
You didnt see the invalid password message because, your else condition is applied only to the innermost if (which checks for the char "T" or "t").
So, if the provided password starts with "BOL" but last char is different like "BOLA" or "BOLB", then you would see the invalid message. But if the first three char fail, then it wont execute the else.
Hope this helps..
I'did use string.matches function which accepts regex as argument. (?i) helps to do a case-insensitive match.
if(string.matches("(?i)bolt"))
{
System.out.println("Valid password");
}
else {System.out.println("InValid password");}
What you are doing is not making much sense. You should probably use a character array an then check sequentially. Another small thing is that the else block is associated with the last if statement. Where it should be attached to the first if. This is one place where you need to use braces intelligently.

Password verifier problems

I am trying to complete my programming assignment, I have to create a "Password verifier" that has 1 uppercase, 1 lowercase, 1 number, and 1 special character. If invalid, it must show "invalid" plus the rule that it has shown as false. The problem I have, is not an error message, but I always get a positive response, plus all the violated rules, plus it reads them twice. Little help here?
import java.util.Scanner;
public class PasswordVerifier {
public static void main(String[] args) {
System.out.println("Password Verifier");
System.out.println("Enter a password that meets the following rules: ");
System.out.println("-Must be at least 8 characters long" + '\n' +
"-Must contain at least 1 lower case character" + '\n' +
"-Must contain at least 1 upper case character" + '\n' +
"-Must contain at least 1 numeric digit" + '\n' +
"-Must contain at least 1 special character from the set: !##$%^&*" + '\n' +
"-Must not contain the word 'and' or the word 'end'");
String password;
String contains1 = "and";
String contains2 = "end";
String special = "!##$%^&*";
Scanner stdIn = new Scanner(System.in);
boolean digit = false; //Has at least 1 digit
boolean upper = true; //Has at least 1 upper case letter
boolean lower = true; //Has at least 1 lower case letter
boolean hasspecial = true; //Has at least 1 special character
boolean length = true; //Has at least 8 digits
boolean endand = true; //Does not contain end or and
boolean valid = false; //Is the password valid?
System.out.println("Enter password: ");
password = stdIn.nextLine();
int result;
result = password.indexOf(contains1);
if (result == -1) {
System.out.println("");
} else {
System.out.println("Must not contain the word 'and'");
}
int result2;
result2 = password.indexOf(contains2);
if (result2 == -1) {
System.out.println("");
} else {
System.out.println("Must not contain the word 'end'");
}
if (password.length() < 8) {
System.out.println("Must be at least 8 characters long.");
} else {
System.out.print("");
}
for (int i = 0; i < password.length(); i++) {
if (!(Character.isUpperCase(password.charAt(i)))) ;
{
upper = false;
valid = false;
i++;
}
if (!(Character.isLowerCase(password.charAt(i)))) ;
{
lower = false;
valid = false;
i++;
}
if (!(Character.isDigit(password.charAt(i)))) ;
{
digit = false;
valid = false;
i++;
}
if (!(password.matches(special))) ;
{
hasspecial = false;
valid = false;
}
if (upper != true) {
System.out.println("Must contain an upper case letter.");
}
if (lower != true) {
System.out.println("Must contain a lower case letter.");
}
if (digit != true) {
System.out.println("Must contain a numeric digit.");
}
if (hasspecial != true) {
System.out.println("Must contain a special character.");
}
if (valid) {
System.out.println("Valid.");
} else if (valid != true) {
System.out.println("Invalid.");
}
}
}
}
One thing. All your if lines are wrong.
You are adding a ';' after the if. That means, if the if condition is met, do nothing, then always execute the following lines.
Another thing, when you do
if (!(password.matches(special))) {
hasspecial = false;
valid = false;
}
You are asking for the password to match the string "!##$%^&(asterisk)", which it probably doesn't. So, since you are assuming that the string contains a special character, hasspecial will always be true. You want to write something like password.matches(".(asterisk)[!##$%^&*].(asterisk)) (Probably escaping some characters).
Finally, you are saying if a letter doesn't meet a condition, set the marker to false. So, if the following letter does meet the condition, the marker will still be false. You should usually approach this kind of problems by assuming that all of the conditions are false and then setting the markers to true once the condition is met.
Note: I wrote (asterisk) instead of *, becasue the text gets highlighted and I don't know how to escape it :(

Beginner: Do while loop issue

Ello, got a minor issue that I haven't got a clue on what's going wrong... Snippet of the code:
do{
String third = JOptionPane.showInputDialog(null, "Please enter Year ["+day+"/"+month+"/YY?]");
year = Integer.parseInt (third);
validChoice = validYear(year);
}while(!validChoice);
do{
String goUS = JOptionPane.showInputDialog(null, "Do you want to switch the format to US? [Y/N]");
goUS = goUS.toUpperCase();
char conversion = goUS.charAt(0);
validChoice = validOption(conversion);
switch(conversion){
case 'Y':
if(year < 10)
{
date.append(month).append("/").append(day).append("/" + "200").append(year);
}else{
date.append(month).append("/").append(day).append("/" + "20").append(year);
}
JOptionPane.showMessageDialog(null, date);
break;
case 'N':
if(year < 10)
{
date.append(day).append("/").append(month).append("/" + "200").append(year);
}else{
date.append(day).append("/").append(month).append("/" + "20").append(year);
}
JOptionPane.showMessageDialog(null, date);
break;
default:
JOptionPane.showMessageDialog(null, "Invalid answer! Use Y/N", "Error", JOptionPane.ERROR_MESSAGE);
}}while(!validChoice);
//// METHODS:
public static boolean validYear (int year){
boolean isValid = true;
if(year < 1 || year > 99)
{
isValid = false;
}
return isValid;
}
public static boolean validOption (char conversion){
boolean isValid = true;
if(conversion != 'y' || conversion != 'n')
{
isValid = false;
}
return isValid;
}
The first part, with regards to the year, and with it's associated method, it loops fine if the input is wrong.
The second part, 'goUS', the method's remotely the same thing, checking the char if it's y/n - it works fine with regards to the check and all, but after hitting OK with the correct display of the date, it starts the goUS loop again, and the answers start to concatenate itself from the previous display, and so on. I want the app to just end after displaying the date.
Any help pointing out my flaw would be greatly appreciated.
Well, your validOption checks for lower case characters, but you pass it upper case characters.
The most flexible solution would be to normalise the case before comparing to expected values, such as
public static boolean validOption (char conversion) {
conversion = Character.toLowerCase(conversion)
return conversion == 'y' || conversion == 'n';
}
It seems the problem is because of case of input character, you are checking with lower-case in validOption and checking with upper-case in the main method. I believe your input is upper case, and validOption returns false.
Change validOption to:
public static boolean validOption (char conversion) {
return conversion == 'Y' || conversion == 'N';
}
or even use java.lang.Character toLower or toUpper methods.
Your validOption() does not work correctly. The working of the rest of the loop is independent of that, but the termination depends on the correct return value.
You check lower case characters there, but uppercase elsewhere. You change the checked character to uppercase just before the validity check. Change the characters to uppercase in validOption() too.

method to check if password/keycode match

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 loop
boolean flag=false;
while(flag!=true)
{
Scanner keycode = new Scanner(System.in);
System.out.println("Input keycode");
String key1 = keycode.nextLine();
if (key1.length() < 6 || key1.length() > 8) {
System.out.println("must be at least 6 letter and max 8 letter");
}
else
{
flag=true;
}
}
Use a boolean variable say redo. And at every place you have used return, replace it with redo = true. And your while condition should be while(redo), which will run until redo is true, that it, user hasn't entered correct number.
Also, move your input reading line: -
String key1 = keycode.nextLine();
Inside your do-while loop. And also ,reset your redo variable to false in the loop, so that it doesn't remain true forever.
String[] keys = new String[2]; // Since you are reading 2 keys, so size = 2
boolean redo = false;
int count = 0; // To maintain the number of keys read. should stop at count = 2
do {
redo = false;
Scanner keycode = new Scanner(System.in);
System.out.println("Input keycode No: + " + (count + 1));
String key1 = keycode.nextLine();
if (key1.length() < 6 || key1.length() > 8) {
redo = true;
} else {
/** Your remaining code **/
if (!upper) {
System.out.println("must contain at least one uppercase character");
redo = true;
} else if (!lower) {
System.out.println("must contain at least one lowercase character");
redo = true;
} else if (!number) {
System.out.println("must contain at least one number");
redo = true;
} else {
keys[count++] = key1; // Enter a new key in array
}
}
} while (redo || count < 2);
if (keys[0].equals(keys[1])) {
System.out.println("Keys are equal");
}
You should put the line String key1 = keycode.nextLine(); inside the do loop so that the user is queried once every time it is needed. Else, you are just stuck in a loop that will always check the same value again and again.
boolean redo = false;
do {
redo = false;
Scanner keycode = new Scanner(System.in);
System.out.println("Input keycode");
String key1 = keycode.nextLine();
if (key1.length() < 6 || key1.length() > 8) {
redo = true;
} else {
/** Your remaining code **/
if (!upper) {
System.out.println("must contain at least one uppercase character");
redo = true;
} else if (!lower) {
System.out.println("must contain at least one lowercase character");
redo = true;
} else if (!number) {
System.out.println("must contain at least one number");
redo = true;
}
else{
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");
}
}
}
} while (redo);
i manage to fit the code inside here , not sure if its the correct method to do so?

Categories

Resources