Java Password Creation

Hi Guys!

So I'm here, and I am trying to have the user put in their password that they have already made(so to say), or one that they are making. Here are my requirements.

  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 the tests below fail, set it to false.
  3. Check the password to see if it has at least eight characters. If it does not, display the message, “Password must have at least eight characters.”
  4. Check the password to see if it consists of only letters and digits. To do this, you will need to loop through all of the characters in the string. A character c is a letter or digit if this expression is true:
    • ('a' <= c && c <= 'z') ||
    • ('A' <= c && c <= 'Z') ||
    • ('0' <= c && c <= '9')
  5. If this is ever not true, break from your loop and display the message, “Password must contain only letters and digits.”
  6. If valid is still true at the end of the program, display the message, “Password accepted!”

For me, some of this is pretty easy. I asked my teacher what would be best used for this: a char[] element, or a string element. She said string would be easier. How would I do that? What would you do, if you had a choice between either one of them. And could I convert a char[] element to a string element. Also, how would I check to see if certain characters are in the password, of which is allowed, and what is not? Here's my code:

import java.util.Scanner;
   
    public class MyClass {
public static void main(String args[]) {
 
  //Create a Scanner
  Scanner input = new Scanner(System.in); 
  
  //Creating a boolean attribute
  boolean valid = true; 
  
  //Prompting the user to enter password of 8 digits
  System.out.println("Enter your password");
  
  char[] password = 8; 
  
  if(char[] password < 8) {
      System.out.println("Please enter the correct password");
  }
  else {
      System.out.println("Welcome!");
  }
  }

}

So, the first thing you need to do is handle the input. I believe you’ve used it before, so we’ll skip that step.

  • So now you need to check the length - a string object has a length() method (i.e. someString.length()) to it. Use that to check for the minimum length
  • that length you just checked can also be used in a for loop for (int i = 0, i < someString.length(): i++)
    • you can use the strings charAt method (i.e. someString.length(i)) to get the value
    • Your teacher already gave you the IF statement, if you replace the c with the charAt from the last step. If the IF statement is true, the character is valid, otherwise change the boolean to false;
Do the work - this doesn't handle keyboard entry but validates the string

public class MyClass {
public static void main(String args) {
// replace ags[0] with a valid keyboard entry…
boolean validPassword = validPassword(args[0]);
}

static boolean validPassword(String str) {
    boolean valid = true;
    if (str.length() < 8) {
        valid = false;
        System.out.println("Password must have at least eight characters");

    } else {
        for (int i = 0; i < str.length(); i++) {
            if (('a' <= str.charAt(i) && str.charAt(i) <= 'z') ||
                ('A' <= str.charAt(i) && str.charAt(i) <= 'Z') ||
                ('0' <= str.charAt(i) && str.charAt(i) <= '9')) {
                    // do nothing valid
                } else {
                    valid = false;
                    System.out.println("Password must contain only letters and digits");
                }
        }    
    }
    if (valid) {
        System.out.println("Password accepted!");
    }
    return valid;
}

}

What does these parts actually do? I’m not sure

I also have another question. When I am trying to pass this, I need to replace what the user input instead of the args[8] function. Here’s the code:

import java.util.Scanner;

public class MyClass {
public static void main(String[] args) {
    
    Scanner input = new Scanner(System.in);

boolean correct = valid(args[8]);
boolean correct = input;
input = valid;
}

static boolean valid(String str) {
    boolean valid = true;
    if (str.length() < 8) {
        valid = false;
        System.out.println("Password must have at least eight characters");

    } else {
        for (int i = 0; i < str.length(); i++) {
            if (('a' <= str.charAt(i) && str.charAt(i) <= 'z') ||
                ('A' <= str.charAt(i) && str.charAt(i) <= 'Z') ||
                ('0' <= str.charAt(i) && str.charAt(i) <= '9')) {
                    // do nothing valid
                } else {
                    valid = false;
                    System.out.println("Password must contain only letters and digits");
                }
        }    
    }
    if (valid) {
        System.out.println("Password accepted!");

When you look at:
boolean correct = valid(args[8]);
boolean correct = input;
input = valid;

That’s really not going to be part of the code. I was trying to do something for my teacher. This is what she’s saying:
you have to pass it a String.

In this line, you tried to do that, but the String you passed was empty: boolean correct = valid(args[8]);

Just replace args[8] with the String variable that you want it to test (what the user entered: input)

Confused here. I’m thinking of Python here, which I don’t think is going to help. that’s what the input = valid was. Said that I was on the right track, but I can’t figure it out. Help, please?

This part is #4 of your requirement. If the character is A-Z,a-z, or 0-9, it does nothing because it’s valid. Otherwise it falls into the else and sets the boolean to false.

 if (('a' <= str.charAt(i) && str.charAt(i) <= 'z') ||
                ('A' <= str.charAt(i) && str.charAt(i) <= 'Z') ||
                ('0' <= str.charAt(i) && str.charAt(i) <= '9')) {
                    // do nothing valid
                }

You could write it like this to make it shorter, but it’s not as easy to read. !(…) means fire if NONE of the code inside is true.

            if (!(('a' <= str.charAt(i) && str.charAt(i) <= 'z') ||
                ('A' <= str.charAt(i) && str.charAt(i) <= 'Z') ||
                ('0' <= str.charAt(i) && str.charAt(i) <= '9'))) {
                    valid = false;
                    System.out.println("Password must contain only letters and digits");
                }

This is a method, which you should know what the purpose of one is by now, correct? The method returns a boolean value, meaning either true or false.

It didn’t need to be in a separate method, but because it’s good practice to have one purpose per method, I made it a method. I did that because if more code needed to be added to that main, it stops having one purpose.

In this particular instance, you can use void and not return anything since it doesn’t need to do anything with it, but a typical form validation WOULD need to know whether the value entered is valid or not, hence why it returns a boolean. Force of habit on my part…

static boolean validPassword(String str) {

Well, yeah. I specifically said in my post (bold for emphasis…)

The code I gave you ran in an online java tester just to be sure it was valid and did what you wanted and was tested using it’s command line arguments.

To get it to work with the keyboard, all you needed to do was copy the method into your code, then add the call to the method right after your declaration of the scanner object, changing this line.

boolean valid = validPassword(args[0]);

to something like (I think - it’s been a while since I’ve done Java)

boolean valid = validPassword(input.nextLine());

Testing my code here, and I got it to work…partially. When I enter the password, and I get it wrong, it’ll say password is wrong. What I’m trying to do is do something like main(args); that I did in my I am confused about how to do this in Java post, and it’s not working. How do I force it to go back, since it’s not working

OK, this is as simple as I can make it. No separate method - just basic input and response. And this works in the link I provided, so I know it works…

import java.util.Scanner;
public class MyClass {
    public static void main(String args[]) {
        boolean valid = true;
        
        System.out.println("Please Enter Password");
        
        Scanner input = new Scanner(System.in);
        String str = input.nextLine();
        
        if (str.length() < 8) {
            valid = false;
            System.out.println("Password must have at least eight characters");
        } else {
            // need the - 1 because nextLine() captures everything INCLUDING the enter key
            for (int i = 0; i < str.length() - 1; i++) {
                if (('a' <= str.charAt(i) && str.charAt(i) <= 'z') ||
                    ('A' <= str.charAt(i) && str.charAt(i) <= 'Z') ||
                    ('0' <= str.charAt(i) && str.charAt(i) <= '9')) {
                        // do nothing valid
                    } else {
                        valid = false;
                        System.out.println("Password must contain only letters and digits");
                    }
            }    
        }
        if (valid) {
            System.out.println("Password accepted!");
        }        
    }
}

Got it working.
New problem, and I think it has something to do with the if statement

image

Not supposed to have the character, ^, and it’s accepting it. Not, though, if the character is in the middle of the password. Does it have to do with the restrictions placed on what it is allowing the error?

i < str.length() - 1

It’s this part of the loop…I must have had <= in the loop, which is why I added the -1. But changing it to this fixes it.

i < str.length()

Would have been easy to find to add a little debug statement in there.

System.out.println(i + ": " + str.charAt(i));

With the -1


Without

Notice how the last character wasn’t being tested…