經過對這段代碼的大量作業,關于 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[]) List errorList = new ArrayList() 中定義;
uj5u.com熱心網友回復:
您在主函式第 91 行和第 102 行中宣告了 errorList 2 次,請注意 Java 是一種強型別語言,一個變數在一個范圍內只能有一個宣告,將第二個宣告更改為 errorList1 或其他內容,它應該可以作業美好的
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/470765.html
