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.
- Write a program that prompts the user to enter a password.
- Create a boolean variable named valid and set it to true. If any of the tests below fail, set it to false.
- 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.”
- 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')
- If this is ever not true, break from your loop and display the message, “Password must contain only letters and digits.”
- 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!");
}
}
}