-3

The below are the instructions and my code that I have tried so far. I am almost done but just having problems with part four. So the expected output should be:

Please enter a password: abc
Password much have at least 8 characters 
Please enter a password: abcd1234$
Password must only contain letter and digits
Please enter a password: ####
Password must have at least 8 characters
Password must only contain letters and digits
Please enter a password: abcd1234
Password accepted!

When I type abc this is what I get:

Please enter password and then hit enter:abc
Password must have at least 8 characters
Password Accepted

When I do this the program ends! Could someone help me with this?

Problem

  1. Write a program that prompts the user to enter a password.
  2. Create a boolean variable named valid and set it to true. If any of these tests below fail, set it to true.
  3. Check the password to see if it has at least 8 characters. If it does not, display the message, "Password must have at least 8 characters"
  4. Check the password to see if it consists of only letter and digits. To do this, you will need to loop through all of the characters in the string. A character c is a letter of digit if this expression is true:

    ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9')

if this is even not true, break from your loop and display the message, "Password must contain only letter and digits" 5. If valid is still true at the end of the program, display the message, "Password accepted!"

My code

    import java.util.Scanner; 
    public class PasswordVerification {

        public static void main(String[] args) {
            //Creates a scanner
            Scanner sc = new Scanner(System.in);
            boolean valid = false; 
            String password;


            //Asks user to enter password
            System.out.print("Please enter password and then hit enter:");
            password = sc.nextLine(); 

            //Checks to see if password is at least 8 characters. 
            if (password.length()<8) 
                {
                    valid = false;
                    System.out.println("Password must have at least 8 characters");
                }

            //Checks each character to see if it is acceptable.
            for (int i = 0; i < password.length(); i++){
                        char c = password.charAt(i);

                        if (       ('a' <= c && c <= 'z') // Checks if it is a lower case letter
                                || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter
                                || ('0' <= c && c <= '9') //Checks to see if it is a digit
                        ) 
                        {

                            valid = true;
                        } 

                        else 
                        {
                            // tells the user that only letters & digits are allowed
                            System.out.println("Only letter & digits are acceptable.");
                            valid = false;
                            break;
                        }

            }

            // if the password is valid, tell the user it's accepted
            System.out.println("Password Accepted");
            }


    }
Pavindu
  • 2,684
  • 6
  • 44
  • 77
BlazeRyder
  • 53
  • 1
  • 2
  • 7
  • 1
    Regex could be very helpful for part 4. Is there a reason you're not using it? Example: http://stackoverflow.com/questions/11241690/regex-for-checking-if-a-string-is-strictly-alphanumeric – Joe Nov 17 '16 at 16:20
  • 2
    you need to add a loop, a whilte for ex, and be all time in there until the password is valid – cralfaro Nov 17 '16 at 16:21
  • @joe Poster is clearly a complete novice. Regex is something to tackle later. – slim Nov 17 '16 at 16:22
  • @Joe This is a Java Programming 1 class, so I only know about beginner things. And I have not heard of Regex at all so I don't know if my teacher will allow me to use it..... – BlazeRyder Nov 17 '16 at 16:24
  • Please include eception. – GC_ Nov 17 '16 at 16:27
  • What do I put in the while loop? Part 3 and 4? @cralfaro – BlazeRyder Nov 17 '16 at 16:28

4 Answers4

2

As @cralfaro stated you have to repeat the process if the password is invalid:

import java.util.Scanner; 
public class PasswordVerification {

    public static void main(String[] args) {
        //      Creates a scanner
        Scanner sc = new Scanner(System.in);
        boolean valid = false; 
        String password;

        do { // start a loop
            //      Asks user to enter password
            System.out.print("Please enter password and then hit enter:");
            password = sc.nextLine(); 

            //      Checks to see if password is at least 8 characters. 
            if (password.length()<8) 
            {
                valid = false;
                System.out.println("Password must have at least 8 characters");
                continue; // skip to next iteration
            }

            //      Checks each character to see if it is acceptable.
            for (int i = 0; i < password.length(); i++){
                char c = password.charAt(i);

                if (       ('a' <= c && c <= 'z') // Checks if it is a lower case letter
                        || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter
                        || ('0' <= c && c <= '9') //Checks to see if it is a digit
                ) 
                {

                    valid = true;
                }
                else 
                {
                    // tells the user that only letters & digits are allowed
                    System.out.println("Only letter & digits are acceptable.");
                    valid = false;
                    break;
                }

            }
        } while(!valid); // verify if the password is valid, if not repeat the process

        // if the password is valid, tell the user it's accepted
        System.out.println("Password Accepted");
    }


}

In this way the program will continue to ask the user input if the password is not valid.

EDIT: Thanks to GC_'s comment, the problem was that I missed a continue statement in the first check.

aleb2000
  • 452
  • 4
  • 10
0

Your problem ist this part of code:

if (    ('a' <= c && c <= 'z') // Checks if it is a lower case letter
     || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter
     || ('0' <= c && c <= '9') //Checks to see if it is a digit
    ) { valid = true; }

Here you reset valid to true, even if the first check with the length being less than 8 already failed. So abc will fail 3. and print Password must have at least 8 characters which is fine. Then it will pass 4., reset valid to true and print Password Accepted.

So you want the replace the if (...) { valid = true; } else { ... } part by

if (!( // if the character is none of the options below, print error
       ('a' <= c && c <= 'z') // Checks if it is a lower case letter
    || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter
    || ('0' <= c && c <= '9') //Checks to see if it is a digit
    )) { 
        // tells the user that only letters & digits are allowed
        System.out.println("Only letter & digits are acceptable.");
        valid = false;
        break; 
    }

Edit: Also you should initially set boolean valid = true; instead of false, as you want to set it to false only if it fails a condition. Then at the end of your code, add a condition checking valid around the last output line, like

if(valid) {
    System.out.println("Password Accepted");
}
Aracurunir
  • 625
  • 6
  • 10
  • When I put this into my code, run it, and put abc as the password this is what I get: Please enter password and then hit enter:abc Password must have at least 8 characters Password Accepted! – BlazeRyder Nov 17 '16 at 16:44
0

A little messy but try this:

public static void main(String[] args) {
//      Creates a scanner
Scanner sc = new Scanner(System.in);
boolean valid = false; 
String password;


//      Asks user to enter password
while(true){
System.out.print("Please enter password and then hit enter:");
password = sc.nextLine(); 

//      Checks to see if password is at least 8 characters. 
if (password.length()<8) 
    {
        valid = false;
        System.out.println("Password must have at least 8 characters");
    }
else {

//      Checks each character to see if it is acceptable.
for (int i = 0; i < password.length(); i++){
            char c = password.charAt(i);

            if (       ('a' <= c && c <= 'z') // Checks if it is a lower case letter
                    || ('A' <= c && c <= 'Z') //Checks if it is an upper case letter
                    || ('0' <= c && c <= '9') //Checks to see if it is a digit
            ) {
                valid = true;
            } else {
                System.out.println("Password denied");
                System.out.println("Only letter & digits are acceptable.");
                valid = false;
                break;
            }


}

if (valid == true) {
System.out.println("Password accepted");
    break;
        }            
}
}
}
Cristophs0n
  • 1,246
  • 3
  • 19
  • 27
  • This seems to be working with looping but it does not work. When I type abc it works as expected, but when I type abcd1234$ it says password accepted. But that should not be the case as it should only be accepted if it has 8 characters( which it does) and all characters are letter and digits(which is false becuase of the $) This is the output in eclipse: Please enter password and then hit enter:abc Password must have at least 8 characters Please enter password and then hit enter:abcd1234$ Password Accepted – BlazeRyder Nov 17 '16 at 16:51
  • Updated it. Try it again – Cristophs0n Nov 17 '16 at 17:03
-1

Add a check to see if the password is still valid before printing that the password is accepted.

if(valid==true) {
    System.out.println("Password Accepted");
}

EDIT: You can also add this.

else {
    System.out.println("Password Denied");
}
Joe
  • 800
  • 4
  • 10
  • 26
  • Oh he does want it in a loop. Also, it should be valid = valid && true; – GC_ Nov 17 '16 at 16:31
  • Your part is needed to. – GC_ Nov 17 '16 at 16:31
  • The OP is missing the do-while loop. Your answer does not provide the correct answer and it's even counter productive. – Murat Karagöz Nov 17 '16 at 16:32
  • @MuratK. Part parts are needed, right now hid program always says pss. – GC_ Nov 17 '16 at 16:33
  • @MuratK. It depends, the OP didn't ask for the do-while loop he asked why his program is printing "Password accepted" so this answer is not wrong, it is simply not the best thing to do in this case. – aleb2000 Nov 17 '16 at 16:34