尝试在 Arraylist 中使用 contain() 时,找不到我要查找的内容
While trying to use contain() in Arraylist it doesn't find what I'm looking
所以我有一个名为 class 的学生,如下所示:
public class Student {
public int usercounter;
public String username,password,studentname;
Student(String studentname ,String username, String password, int usercounter){
this.studentname = studentname;
this.username = username;
this.password = password;
this.usercounter = usercounter;
}
我正在尝试使用 Arraylists 编写图书馆登录代码。
static ArrayList<Student> users = new ArrayList<Student>();
我的主菜单是这样的:
public static void MainMenu() {
while (menubreak){
System.out.println("ULIS Main Menu\nPlease choose the feature you want to access:\n1-Login\n2-Create User\n3-Delete User\n4-Exit");
gir = Input.nextInt();
Input.nextLine();
switch (gir) {
case 1:
Login();
break;
我这样创建用户:
public static void CreateUser() {
while (userbreak) {
System.out.println("You are creating a new User");
System.out.println("Please enter the name of the Student:");
tempStudentname = Input.nextLine();
System.out.println("Please enter a new username:");
tempUsername = Input.nextLine();
System.out.println("Please enter a new password:");
tempPassword = Input.nextLine();
users.add(new Student(tempStudentname, tempUsername, tempPassword, usercounter));
usercounter++;
我的登录屏幕如下所示:
public static void Login() {
System.out.println("Please choose the user type you want to login:\n1-Admin\n2-Student");
giris = Input.nextInt(); Input.nextLine();
switch (giris) {
case 2:
System.out.println("Please enter your username:");
tempUsername = Input.nextLine();
searchUsername = tempUsername;
System.out.println("Please enter your password:");
tempPassword = Input.nextLine();
if(users.contains(searchUsername) && users.contains(tempPassword) ){
System.out.println("User found, logging in now");
UserMenu();
}
else{
System.out.println("User cannot be found, returning to Main Menu");
}
}
}
当我 运行 出现这个时,它会出现“找不到用户”
我不允许使用离线数据库或类似的东西,这就是为什么我在旅途中创建用户名并且不将它们存储在某些东西中。
它找不到要查找的用户名和密码的原因可能是什么?顺便说一句,我是一年级学生,如果你能尝试在不使用复杂技术的情况下解释事情
您当前正在将 Student
对象与学生姓名进行比较,但有一个简单的解决方法,假设我们有以下代码:
List<Student> students = new ArrayList<>();
students.add(new Student("Bob", "password"));
students.add(new Student("Alice","wonderland"));
然后当您从用户输入中得到 tempUsername
和 tempPassword
时,您可以遍历列表并检查是否有具有该名称和密码的学生:
Student loggedIn = null;
for(Student student : students){
if(student.getName().equals(tempUsername) && student.checkPassword(tempPassword)){
loggedIn = student;
break;
}
}
(注意这里我给student添加了一些getter和setter方法,为了更好的封装现在看起来像这样)
class Student {
private String name;
private String password;
public Student(String name, String password){
this.name = name;
this.password = password;
}
public String getName(){
return name;
}
public boolean checkPassword(String password){
return this.password.equals(password);
}
}
如果您被特别要求使用 ArrayList
来执行此操作,那很好,但是如果您可以使用其他数据结构,您可以考虑使用 HashMap
s(您可以使用 HashMap<String,Student>
你可以在其中传递学生的用户名,如果它在 HashMap
中则取回学生,这将是更好的时间复杂度,或者更好的是,定义你自己的可比较接口并使用 HashSet
,这样你就可以比较用户名,这也有助于防止添加具有相同用户名的用户。
所以我有一个名为 class 的学生,如下所示:
public class Student {
public int usercounter;
public String username,password,studentname;
Student(String studentname ,String username, String password, int usercounter){
this.studentname = studentname;
this.username = username;
this.password = password;
this.usercounter = usercounter;
}
我正在尝试使用 Arraylists 编写图书馆登录代码。
static ArrayList<Student> users = new ArrayList<Student>();
我的主菜单是这样的:
public static void MainMenu() {
while (menubreak){
System.out.println("ULIS Main Menu\nPlease choose the feature you want to access:\n1-Login\n2-Create User\n3-Delete User\n4-Exit");
gir = Input.nextInt();
Input.nextLine();
switch (gir) {
case 1:
Login();
break;
我这样创建用户:
public static void CreateUser() {
while (userbreak) {
System.out.println("You are creating a new User");
System.out.println("Please enter the name of the Student:");
tempStudentname = Input.nextLine();
System.out.println("Please enter a new username:");
tempUsername = Input.nextLine();
System.out.println("Please enter a new password:");
tempPassword = Input.nextLine();
users.add(new Student(tempStudentname, tempUsername, tempPassword, usercounter));
usercounter++;
我的登录屏幕如下所示:
public static void Login() {
System.out.println("Please choose the user type you want to login:\n1-Admin\n2-Student");
giris = Input.nextInt(); Input.nextLine();
switch (giris) {
case 2:
System.out.println("Please enter your username:");
tempUsername = Input.nextLine();
searchUsername = tempUsername;
System.out.println("Please enter your password:");
tempPassword = Input.nextLine();
if(users.contains(searchUsername) && users.contains(tempPassword) ){
System.out.println("User found, logging in now");
UserMenu();
}
else{
System.out.println("User cannot be found, returning to Main Menu");
}
}
}
当我 运行 出现这个时,它会出现“找不到用户” 我不允许使用离线数据库或类似的东西,这就是为什么我在旅途中创建用户名并且不将它们存储在某些东西中。 它找不到要查找的用户名和密码的原因可能是什么?顺便说一句,我是一年级学生,如果你能尝试在不使用复杂技术的情况下解释事情
您当前正在将 Student
对象与学生姓名进行比较,但有一个简单的解决方法,假设我们有以下代码:
List<Student> students = new ArrayList<>();
students.add(new Student("Bob", "password"));
students.add(new Student("Alice","wonderland"));
然后当您从用户输入中得到 tempUsername
和 tempPassword
时,您可以遍历列表并检查是否有具有该名称和密码的学生:
Student loggedIn = null;
for(Student student : students){
if(student.getName().equals(tempUsername) && student.checkPassword(tempPassword)){
loggedIn = student;
break;
}
}
(注意这里我给student添加了一些getter和setter方法,为了更好的封装现在看起来像这样)
class Student {
private String name;
private String password;
public Student(String name, String password){
this.name = name;
this.password = password;
}
public String getName(){
return name;
}
public boolean checkPassword(String password){
return this.password.equals(password);
}
}
如果您被特别要求使用 ArrayList
来执行此操作,那很好,但是如果您可以使用其他数据结构,您可以考虑使用 HashMap
s(您可以使用 HashMap<String,Student>
你可以在其中传递学生的用户名,如果它在 HashMap
中则取回学生,这将是更好的时间复杂度,或者更好的是,定义你自己的可比较接口并使用 HashSet
,这样你就可以比较用户名,这也有助于防止添加具有相同用户名的用户。