Java登录开发遇类型不匹配错误,求比较器实现指导
Hey there! Let's work through your two main problems: the type mismatch error and implementing the login comparison logic. I'll break this down step by step.
1. Fixing the "incompatible types" Error
The root cause here is a mismatch between what your User constructor expects and what you're passing it:
- Your
Userclass has a constructor that takes two String parameters:public User(String username, String password) - But in
UserList, you're trying to pass anintas the second argument withInteger.parseInt(res[1])
Since your Logins.txt stores passwords as strings (like "Password"), you don't need to convert it to an integer. Additionally, your file has a space after the comma (e.g., Test, Password), so splitting on commas will leave a leading space in your password string—we'll fix that with trim() to avoid mismatches later.
Here's the corrected UserList class:
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; public class UserList { private ArrayList<User> loginList; // Initialize the list in the constructor instead of taking it as a parameter public UserList() { loginList = new ArrayList<>(); Scanner sc; try { sc = new Scanner(new File("F:\\Cours\\[ISIS 2A]\\Projet Programmation\\BoiteDeJeux\\src\\boitedejeux\\Logins.txt")); // Fix: Don't skip the first line with an extra sc.nextLine() while (sc.hasNextLine()) { String ligne = sc.nextLine(); String[] res = ligne.split(","); // Trim to remove leading/trailing spaces from username and password String username = res[0].trim(); String password = res[1].trim(); // Pass two Strings to the User constructor (no Integer conversion!) loginList.add(new User(username, password)); } } catch (FileNotFoundException e) { System.out.println("Error: Could not find Logins.txt file."); e.printStackTrace(); } } // Add a getter to access the login list from other classes public ArrayList<User> getLoginList() { return loginList; } }
Key fixes here:
- Removed the
ArrayList<User>parameter from theUserListconstructor and initialized the list internally - Fixed the loop to not skip the first line of your file
- Used
trim()to clean up whitespace from the split values - Removed
Integer.parseInt()for the password, passing the cleaned string directly to theUserconstructor
2. Implementing the Login Comparison Logic
Now we need to add the loginCheck method to your BoiteDeJeux class. This method will:
- Create a
UserListinstance to load all stored credentials - Loop through each user in the list
- Compare the user's input against stored username and password
- Return/print a success or failure message
Here's the updated BoiteDeJeux class:
import java.util.Scanner; public class BoiteDeJeux { public static void main(String[] args) { Scanner input = new Scanner(System.in); String username; String password; System.out.print("Enter your username: "); username = input.nextLine().trim(); // Trim user input to avoid whitespace issues System.out.print("Enter your password: "); password = input.nextLine().trim(); loginCheck(username, password); input.close(); // Don't forget to close the scanner! } private static void loginCheck(String inputUsername, String inputPassword) { UserList userList = new UserList(); boolean isLoggedIn = false; for (User user : userList.getLoginList()) { // Compare both username and password if (user.getUsername().equals(inputUsername) && user.getPassword().equals(inputPassword)) { isLoggedIn = true; break; // Exit the loop once a match is found } } if (isLoggedIn) { System.out.println("Login successful! Welcome back."); } else { System.out.println("Login failed: Invalid username or password."); } } }
3. Quick Note on Your Logins.txt
Make sure each credential pair is on a separate line (your current example shows Test, Password Test2, Password2 which might be a typo). It should look like this:
Test, Password Test2, Password2
That's it! With these changes, your type mismatch error will be gone, and the login logic will correctly compare user input against the stored credentials.
内容的提问来源于stack exchange,提问作者Tameiki

