Question

Create a program via Java that has atleast 8 characters long, one upper case, and one...

Create a program via Java that has atleast 8 characters long, one upper case, and one lower case, and one digit.

here is my code:

    public static void main(String[] args)
    {
        //variables defined
        String passwords;
        char password =0;
       
        //settings up password condition to false
        boolean passwordcondition= false;
        boolean size=false;
        boolean digit=false;
        boolean upper=false;
        boolean lower=false;

        //inputting a scanner into the system
        Scanner keyboard = new Scanner(System.in);
       
            System.out.println("Please enter a password: \n (Caution must have one uppercase, one lowercase, and a digit for password to be valid!)");
            passwords=keyboard.nextLine();
      
            while(!passwordcondition)
            {

                        for (int p=0; p<passwords.length(); p++)
                        {  
                                if(Character.isDigit(p))
                                {
                                  digit = true;  
                                }
                                else if(Character.isUpperCase(p))
                                {
                                  upper = true;
                                }
                                else if(Character.isLowerCase(p))
                                {
                                  lower = true;
                                }
                                else if(passwords.length() > 8)
                                {
                                  size = true;
                                }
                        }
                       
                       
                           if(digit == true && upper == true && lower == true && size == true)
                           {
                             System.out.printf("You have entered a valid password");
                             System.exit(0);
                           }
                           else
                           {
                             System.out.println("You have entered an invalid password");
                             return;
                           }

                       
            }

    }

my problem is I cannot get it back into the loop if I have an invalid answer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// I have edited the program as required

import java.util.Scanner;
public class pass
{

public static void main(String[] args)
{
//variables defined
String passwords;
char password =0;

//settings up password condition to false
boolean passwordcondition;
boolean size=false;
boolean digit=false;
boolean upper=false;
boolean lower=false;
//inputting a scanner into the system
Scanner keyboard = new Scanner(System.in);

do
{
System.out.println("Please enter a password: \n (Caution must have one uppercase, one lowercase, and a digit for password to be valid!)");
passwords=keyboard.nextLine();
char ch;

for (int p=0; p<passwords.length(); p++)
{
ch=passwords.charAt(p);
if(Character.isDigit(ch))
{
digit = true;  
}
else if(Character.isUpperCase(ch))
{
upper = true;
}
else if(Character.isLowerCase(ch))
{
lower = true;
}
else if(passwords.length() >= 8)
{
size = true;
}
}


if(digit == true && upper == true && lower == true && size == true)
{
System.out.printf("You have entered a valid password");
passwordcondition=true;
System.exit(0);

}
else
{
System.out.println("You have entered an invalid password");
passwordcondition=false;
}

}while(passwordcondition==false);
}
}

Add a comment
Know the answer?
Add Answer to:
Create a program via Java that has atleast 8 characters long, one upper case, and one...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I just started working on some code for a password creator. I was doing some testing and I keep g...

    I just started working on some code for a password creator. I was doing some testing and I keep getting this error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6 at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47) at java.base/java.lang.String.charAt(String.java:693) at PasswordCreater.check(PasswordCreater.java:56) at Password.main(Password.java:23 In my code I am having the user create a password that is at least 6 characters long, but no more than 16. Also, include at least one lowercase letter, one uppercase letter, one number digit and one special...

  • I am creating a program that will allow users to sign in with a username and...

    I am creating a program that will allow users to sign in with a username and password. Their information is saved in a text file. The information in the text file is saved as such: Username Password I have created a method that will take the text file and convert into an array list. Once the username and password is found, it will be removed from the arraylist and will give the user an opportunity to sign in with a...

  • Write Java program: • Is at least 8 characters long • Contains at least 1 lower letter charact...

    Write Java program: • Is at least 8 characters long • Contains at least 1 lower letter character • Contains at least 1 upper letter character • Contains at least 1 numeric digit • Contains at least 1 special character from the set: !@#$%^&* • Does not contain the word “and” or the word “the” Prompts the user for a password, including the requirements above in your output. Output a string that states valid or invalid. If invalid, state which...

  • Design and implement a Java program (name it PasswordTest) that accepts a string from the user...

    Design and implement a Java program (name it PasswordTest) that accepts a string from the user and evaluates it as a password, giving it a valid or invalid verdict A good password will be at least 8 characters and includes at least one lower-case letter, at least one upper-case letter, at least one digit, and at least one character that is neither a letter nor a digit. Your program will need to check each character in the string in order...

  • Password Validation Write a program called PasswordValidationYourLastName that validates a new password, following these guidelines: Must...

    Password Validation Write a program called PasswordValidationYourLastName that validates a new password, following these guidelines: Must be at least 8 characters Must have at least 1 uppercase letter Must have at least 1 lowercase letter Must have at least 1 digit Write a program that asks for a password, and then asks again to confirm it (you can use the scanner or JOptionPane). Pass those two Strings to a method called validatePassword( ). Your validatePassword( ) should return a boolean...

  • password has to be exactly 8 characters long. Must use: upper case letters- A, B, C,D...Z...

    password has to be exactly 8 characters long. Must use: upper case letters- A, B, C,D...Z and lower case letters- a, b,c, d ..., z and digits- 0, 1,2, ..., 9 If A is the set of passwords with NO upper case letter and B is the set of passwords with NO lower case letter, then what is the size of |A ∪ B|?

  • Im try to create a java program that checks to see if a given boolean expression...

    Im try to create a java program that checks to see if a given boolean expression is a tautology or not my code so far is as follows: public static class TreeNode    {        char data;        TreeNode left;        TreeNode right;               TreeNode(char item)        {            data = item;            left = null;            right = null;        }    } public static...

  • Create a simple Java class for a Password with the following requirements:  This program will...

    Create a simple Java class for a Password with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  One String property: password (protected to only allow secure passwords) o A secure password must be at least 8 characters in length o A secure password must have three of the following four requirements:  A lower case letter ...

  • Here is the skeleton of the password checking program. We have the character counting function (you...

    Here is the skeleton of the password checking program. We have the character counting function (you will need to finish it), and a function to check if the password is good. Change the password algorithm so that (three of the four) two uppercase, two lowercase, two digits, or one other is present. Also change the required length of the password to 10. Bonus points for making the program loop until a good password is entered. You will need to read...

  • Need help debugging. Create an application that keeps track of the items that a wizard can...

    Need help debugging. Create an application that keeps track of the items that a wizard can carry. console application which has no errors: import java.util.Scanner; public class Console {        private static Scanner sc = new Scanner(System.in);     public static String getString(String prompt) {         System.out.print(prompt);         String s = sc.nextLine();         return s;     }     public static int getInt(String prompt) {         int i = 0;         boolean isValid = false;         while (!isValid) {             System.out.print(prompt);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT