Java – Error “void is an invalid type for the variable passwordMethod1”

Error “void is an invalid type for the variable passwordMethod1″… here is a solution to the problem.

Error “void is an invalid type for the variable passwordMethod1”

  • Description: The program checks passwords between 7 and 16
  • Contains only characters, numbers, and underscores
  • and the first character */

Import java.util.*;

public class PasswordValidationOne { 
 /**my hints on how to solve this problem
 * the length of the string must not less 7 and must not more than 16 character
 **/
public static void main (String[] args) {
      Scanner scan = new Scanner(System.in);
      System.out.println("Enter a password");
      String password = scan.nextLine();

public  void  passwordMethod1( password) {
            int lengthPassword = password.length();  getting the length of the string password
            boolean containsDigit = password.matches("*.\\d*.");
            boolean containsUnderscore = password.contains("_");
            char firstCharacter = password.charAt(0);
            if((lengthPassword >= 7&& lengthPassword<=16)&& (containsDigit == true) && (containsUnderscore == true)&& (firstCharacter >= 'A' && firstCharacter <= 'z') )
                System.out.println("Password Accepted! You can proceed");
            else
                System.out.println("Password not accepted! Retry");
      } //end of method passwordMethod1()

}// end of main method

}//End of class

Solution

The error is correct, if a little misleading (obviously). This

public  void  passwordMethod1( password) {

The type of password you want is missing

public  void  passwordMethod1(String password) {

Also, you cannot declare a method inside another method (move the block outside of main).

Related Problems and Solutions