newbieprogrammer
newbieprogrammer

Reputation: 878

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

Upvotes: 0

Views: 2142

Answers (4)

Rohit Jain
Rohit Jain

Reputation: 213311

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");
}

Upvotes: 0

newbieprogrammer
newbieprogrammer

Reputation: 878

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?

Upvotes: 0

dotvav
dotvav

Reputation: 2848

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.

Upvotes: 0

Bhavik Shah
Bhavik Shah

Reputation: 5183

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;
    }
}

Upvotes: 2

Related Questions