0

I am trying to make a program that tests whether or not a password is valid or invalid. After the password is submitted, I would like to give the user a choice to re-run the program(by responding 'y'). Everything works in my code, except when y is pressed at the end, the program stops instead of starting over.

Any help would be greatly appreciated.

import java.util.Scanner;
import java.lang.*;
public class PasswordTest1
{
//Program main method
   public static void main(String[] args)
   {
      Scanner scan = new Scanner(System.in);
      
      String again = "y";
      
      
   //Promt user to enter input
      System.out.print("Enter a password: ");
      String password = scan.nextLine();
   
   //If-else to print whether the password is valid
      if (checkPassword(password)) 
      {
         System.out.println("Valid Password");
         System.out.println("Re-run Program (y/n)? ");
         again = scan.next(); 
      } else {
         System.out.println("Invalid Password");
         System.out.println("Re-run Program (y/n)? ");
         again = scan.next(); 
      }
   
   }

//Program new method

   public static boolean checkPassword(String password) {
   String again = "y";
   while((again == "Y") || (again == "y")){
      boolean checkPassword = true;
      {
      
      //Make sure password is at least 7 digits
         if (password.length() < 8) {
            checkPassword = false; 
         }
         else {
         
         //A password has at least 1 digit
            int numberOfDigit = 0;
            for (int i = 0; i < password.length(); i++) {
               if (isDigit(password.charAt(i))) {
                  if(isDigit(password.charAt(i))){
                     numberOfDigit++;
                  }
               }
            }
            if (numberOfDigit < 1) {
               checkPassword = false;
            }
         //Check for lower case
            int numberOfLowerCase = 0;
            for (int i = 0; i < password.length(); i++) {
               if (isLowerCase(password.charAt(i))) {
                  if(isLowerCase(password.charAt(i))){
                     numberOfLowerCase++;
                  }
               }
            }
            
            if (numberOfLowerCase < 1) {
               checkPassword = false;
            }
            //Check for upper case
            int numberOfUpperCase = 0;
            for (int i = 0; i < password.length(); i++) {
               if (isUpperCase(password.charAt(i))) {
                  if(isUpperCase(password.charAt(i))){
                     numberOfUpperCase++;
                  }
               }
            }
               
            if (numberOfUpperCase < 1) {
               checkPassword = false;
            }
               //Check for Special Characters
            int numberOfSpecialCharacters = 0;
            String specialCharactersString = "!@#$%^&*()'+,-./:;<=>?[]_{}|";
            for (int i = 0; i < password.length(); i++) {
               char ch = password.charAt(i);
               if(specialCharactersString.contains(Character.toString(ch))){
                  numberOfSpecialCharacters++;
               }
            }
               
            if (numberOfSpecialCharacters < 1) {
               checkPassword = false;
            }
            
         
         
         
         }
      
      
      }
      return checkPassword;
   }
   
   return true;
   }
   
   
   public static boolean isDigit(char ch) {
      return (ch <= '9' && ch >= '0');
   }

   public static boolean isLowerCase (char ch) {
      return (ch <= 'z' && ch >= 'a');
   }

   public static boolean isUpperCase (char ch) {
      return (ch <= 'Z' && ch >= 'A');
   }
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
User1959
  • 1
  • 3
  • [tag:javascript] is a completely different language and (in most cases) environment than [tag:java]. Please don't use irrelevant tags on questions. :-) – T.J. Crowder Sep 16 '20 at 18:06
  • 2
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – PM 77-1 Sep 16 '20 at 18:07

1 Answers1

0

I was able to get your code repeat as long as the 'n' key is not pressed. Right now if any key is pressed other than 'n', it will continue.

One problem was that your while loop needs to be inside of your main. The way it is setup, as soon as the checkPassword returns, there is no way for the program to repeat. Asking for y/n had no way to recall the function. You also had an issue with your string comparison, as @PM 77-1 mentioned in the comments. I would recommend keeping the curly brackets spaced out so it is easier to read, you had a pair of brackets that served no purpose.

import java.util.Scanner;
import java.lang.*;
public class PasswordTest1
{
    //Program main method
    public static void main(String[] args)
    {
        String again;
        String n = "n";
        
        while(true)
        {
            //Prompt user to enter input
            Scanner scan = new Scanner(System.in);
            System.out.println("\nEnter a password: ");
            String password = scan.nextLine();
            
            //If-else to print whether the password is valid
            if (checkPassword(password)) 
            {
                System.out.println("\nValid Password");
                System.out.println("Re-run Program (y/n)? ");
                again = scan.next();
                
            }
            else
            {
                System.out.println("\nInvalid Password");
                System.out.println("Re-run Program (y/n)? ");
                again = scan.next();
            }
            
            if(again.equals(n))
            {
                break;
            }
        }
    }

    //Program new method
    public static boolean checkPassword(String password)
    {
        boolean checkPassword = true;
  
        //Make sure password is at least 7 digits
        if (password.length() < 8)
        {
            checkPassword = false; 
        }
        else
        {
            //A password has at least 1 digit
            int numberOfDigit = 0;
            for (int i = 0; i < password.length(); i++)
            {
                if (isDigit(password.charAt(i)))
                {
                    if(isDigit(password.charAt(i)))
                    {
                        numberOfDigit++;
                    }
                }
            }
            if (numberOfDigit < 1)
            {
                checkPassword = false;
            }
    
            //Check for lower case
            int numberOfLowerCase = 0;
            for (int i = 0; i < password.length(); i++)
            {
                if (isLowerCase(password.charAt(i)))
                {
                    if(isLowerCase(password.charAt(i)))
                    {
                        numberOfLowerCase++;
                    }
                }
            }
            if (numberOfLowerCase < 1)
            {
               checkPassword = false;
            }
            
            //Check for upper case
            int numberOfUpperCase = 0;
            for (int i = 0; i < password.length(); i++)
            {
                if (isUpperCase(password.charAt(i)))
                {
                    if(isUpperCase(password.charAt(i)))
                    {
                        numberOfUpperCase++;
                    }
                }
            }
            if (numberOfUpperCase < 1)
            {
                checkPassword = false;
            }
           
            //Check for Special Characters
            int numberOfSpecialCharacters = 0;
            String specialCharactersString = "!@#$%^&*()'+,-./:;<=>?[]_{}|";
            for (int i = 0; i < password.length(); i++)
            {
                char ch = password.charAt(i);
                if(specialCharactersString.contains(Character.toString(ch)))
                {
                    numberOfSpecialCharacters++;
                }
            }
            if (numberOfSpecialCharacters < 1)
            {
                checkPassword = false;
            }
        }
        return checkPassword;   
    }
   
   
    public static boolean isDigit(char ch)
    {
        return (ch <= '9' && ch >= '0');
    }

    public static boolean isLowerCase (char ch)
    {
        return (ch <= 'z' && ch >= 'a');
    }

   public static boolean isUpperCase (char ch)
    {
        return (ch <= 'Z' && ch >= 'A');
    }
}
MFerguson
  • 1,739
  • 9
  • 17
  • 30