After much work on this code, regarding the checkUserPassword method. I can 1 error and cannot seem to find what the problem is. What must be done to fix my code? I am also not understanding what this error message means
import java.util.Scanner; // Import the Scanner class
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.Pattern;
class Main{
//checkUserName Method
static boolean checkUserName(String userName) {
boolean underscore = userName.contains("_");
if (userName.length() <= 5 && underscore == true) {
System.out.println("Username successfully captured \n");
return true;
}
else{
System.out.println("Username is not correctly formatted, please ensure that your username contains an underscore and is no more than 5 characters in length \n");
}
return false;
}
//checkUserPassword Method (At least eight characters long, Contain a capital letter, Contain a number, Contain a special character)
static boolean checkUserPassword(String password, List<String> errorList) {
Pattern specailCharPatten = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
Pattern UpperCasePatten = Pattern.compile("[A-Z ]");
Pattern lowerCasePatten = Pattern.compile("[a-z ]");
Pattern digitCasePatten = Pattern.compile("[0-9 ]");
errorList.clear();
boolean flag=true;
if (password.length() < 8) {
errorList.add("Password lenght must have alleast 8 character !!");
flag=false;
}
if (!specailCharPatten.matcher(password).find()) {
errorList.add("Password must have atleast one specail character !!");
flag=false;
}
if (!UpperCasePatten.matcher(password).find()) {
errorList.add("Password must have atleast one uppercase character !!");
flag=false;
}
if (!lowerCasePatten.matcher(password).find()) {
errorList.add("Password must have atleast one lowercase character !!");
flag=false;
}
if (!digitCasePatten.matcher(password).find()) {
errorList.add("Password must have atleast one digit character !!");
flag=false;
}
return flag;
}
//registerUser method
static Boolean registerUser(){
return true;
}
//
//loginUser method
static Boolean loginUser(){
return true;
}
//
//returnLoginStatus method
static Boolean returnLoginStatus(){
return true;
}
//
public static void main(String[] args) {
System.out.println("Welcome to the User Register program! \n");
Scanner firstname1 = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter firstname: ");
String firstName;
firstName = firstname1.nextLine(); // Read user input from keyboard
Scanner surname1 = new Scanner(System.in); // Create a Scanner objec
System.out.println("Enter surname: ");
String surName;
surName = surname1.nextLine(); // Read user input from keyboard
List<String> errorList = new ArrayList<String>();
Scanner name1 = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter username: ");
String userName;
userName = name1.nextLine(); // Read user input
checkUserName(userName);// call method
Scanner passw1= new Scanner(System.in);// Create a Scanner object
System.out.println("Enter password: ");
String password;
password= passw1.nextLine(); //Read user input
List<String> errorList = new ArrayList<String>();
checkUserPassword("hello", errorList);
System.out.println(errorList);// call method
System.out.println("Welcome " firstName " " surName " it is great to see you.");
}
}
The error I get is; Main.java:102: error: variable errorList is already defined in method main(String[]) List errorList = new ArrayList();
CodePudding user response:
You are declaring errorList 2 times in your main function, line 91 and line 102, Note that Java is a strongly typed language and one variable can have only one declaration in a scope, change the second one to errorList1 or something else and it should work fine