如何修复 checkUserPassword 方法的错误?
How do I fix error for checkUserPassword method?
在对这段代码进行了大量工作之后,关于 checkUserPassword 方法。我可以 1 错误,似乎无法找到问题所在。必须做什么才能修复我的代码? 我也不明白这个错误信息是什么意思
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.");
}
}
我得到的错误是;
Main.java:102: 错误:变量 errorList 已经在方法 main(String[]) 中定义
列表 errorList = new ArrayList();
您在第 91 行和第 102 行的主函数中声明了 errorList 2 次,
请注意,Java 是一种强类型语言,一个变量在一个范围内只能有一个声明,将第二个声明更改为 errorList1 或其他内容,它应该可以正常工作
在对这段代码进行了大量工作之后,关于 checkUserPassword 方法。我可以 1 错误,似乎无法找到问题所在。必须做什么才能修复我的代码? 我也不明白这个错误信息是什么意思
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.");
}
}
我得到的错误是; Main.java:102: 错误:变量 errorList 已经在方法 main(String[]) 中定义 列表 errorList = new ArrayList();
您在第 91 行和第 102 行的主函数中声明了 errorList 2 次, 请注意,Java 是一种强类型语言,一个变量在一个范围内只能有一个声明,将第二个声明更改为 errorList1 或其他内容,它应该可以正常工作