Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用于身份验证的链接列表_Java_Linked List - Fatal编程技术网

Java 用于身份验证的链接列表

Java 用于身份验证的链接列表,java,linked-list,Java,Linked List,我有下面的代码,我试图将用户名和密码存储在一个链接列表中&根据用户从扫描仪输入的内容进行身份验证,但似乎无法使其工作。任何hep都将不胜感激 import java.io.IOException; import java.util.LinkedList; //Linked lists declared to store, auctions, users, and items public class System1{ private static LinkedList<User

我有下面的代码,我试图将用户名和密码存储在一个链接列表中&根据用户从扫描仪输入的内容进行身份验证,但似乎无法使其工作。任何hep都将不胜感激

 import java.io.IOException;
 import java.util.LinkedList;

 //Linked lists declared to store, auctions, users, and items
 public class System1{

private static LinkedList<User> users = new LinkedList<User>();
public static String username;
public static String password;

     public static void main(String[] args) throws IOException,       InterruptedException {

    //User.setuser();

    String username = "user";
    String password = "pw123";

    users.add(new User("user", "pw123"));
    users.add(new User("user1", "pw123"));
    users.add(new User("user2", "pw123"));


    User.validateLogIn(users, username, password);

    System.out.println("If true you are logged in:" + User.userLogedIn);
import java.io.IOException;
导入java.util.LinkedList;
//声明用于存储、拍卖、用户和项目的链接列表
公共类系统1{
私有静态LinkedList用户=新建LinkedList();
公共静态字符串用户名;
公共静态字符串密码;
公共静态void main(字符串[]args)引发IOException、InterruptedException{
//setuser();
字符串username=“user”;
字符串密码=“pw123”;
添加(新用户(“用户”,“pw123”));
添加(新用户(“user1”、“pw123”);
添加(新用户(“user2”、“pw123”);
验证登录(用户、用户名、密码);
System.out.println(“如果为true,则登录:“+User.userLogedIn”);
二等舱

public class User {

public User(String username, String password) {
}
static boolean userLogedIn;

public static void validateLogIn( LinkedList<User> users, String username,      String password) {

Iterator<User> spin = users.iterator();
while (spin.hasNext()){
    User user = spin.next();

    //System.out.println("Searching");
    //System.out.println(username);
    if(username.equals(user.setUsername()) && password.equals(user.setPassword())) {
        //System.out.println("BINGO!");
        userLogedIn=true;
    } else {
        //return false;
        //System.out.println("User not found" + "\n");
    }
    }
 }

private String setUsername() {
String username = "user1";
System.out.println("Enter your username: ");
 Scanner scanner = new Scanner(System.in);
username = scanner.nextLine();

return username;
}

private String setPassword() {
String password = "pw123";
return password ;
}

/*public static void setuser(){
 System.out.println("Enter your username: ");
 Scanner scanner = new Scanner(System.in);
 System1.username = scanner.nextLine();
 System.out.println("Your username is " + System1.username);
 System.out.println("Enter your password: ");
 Scanner scanner1 = new Scanner(System.in);
 System1.password = scanner1.nextLine();
 System.out.println("Your password is " + System1.password);
 }*/
公共类用户{
公共用户(字符串用户名、字符串密码){
}
静态布尔userLogedIn;
公共静态void validateLogIn(LinkedList用户、字符串用户名、字符串密码){
迭代器spin=users.Iterator();
while(spin.hasNext()){
User=spin.next();
//System.out.println(“搜索”);
//System.out.println(用户名);
if(username.equals(user.setUsername())和&password.equals(user.setPassword())){
//System.out.println(“宾果!”);
userLogedIn=true;
}否则{
//返回false;
//System.out.println(“未找到用户”+“\n”);
}
}
}
私有字符串setUsername(){
字符串username=“user1”;
System.out.println(“输入您的用户名:”);
扫描仪=新的扫描仪(System.in);
用户名=scanner.nextLine();
返回用户名;
}
私有字符串setPassword(){
字符串密码=“pw123”;
返回密码;
}
/*公共静态void setuser(){
System.out.println(“输入您的用户名:”);
扫描仪=新的扫描仪(System.in);
System1.username=scanner.nextLine();
System.out.println(“您的用户名是”+System1.username);
System.out.println(“输入密码:”);
扫描仪scanner1=新扫描仪(System.in);
System1.password=scanner1.nextLine();
System.out.println(“您的密码是”+System1.password);
}*/

首先,您需要存储添加到链接列表中的用户名和密码(在用户类生成器中)

其次,不清楚您在验证中要做什么。您不需要传递所有这些参数,LinkedList id就足够了

大概是这样的: System1.java:

 import java.io.IOException;
 import java.util.LinkedList;

 //Linked lists declared to store, auctions, users, and items
 public class System1{

    private static LinkedList<User> users;

    public static void main(String[] args) throws IOException, InterruptedException {

    users = new LinkedList<User>()
    users.add(new User("user", "pw123"));
    users.add(new User("user1", "pw123"));
    users.add(new User("user2", "pw123"));

    User.validateLogIn(users);

    System.out.println("If true you are logged in:" + User.userLogedIn);
}
import java.io.IOException;
导入java.util.LinkedList;
//声明用于存储、拍卖、用户和项目的链接列表
公共类系统1{
私有静态LinkedList用户;
公共静态void main(字符串[]args)引发IOException、InterruptedException{
users=newlinkedlist()
添加(新用户(“用户”,“pw123”));
添加(新用户(“user1”、“pw123”);
添加(新用户(“user2”、“pw123”);
User.validateLogIn(用户);
System.out.println(“如果为true,则登录:“+User.userLogedIn”);
}
User.java:

导入java.util.Scanner

公共类用户{

private String userName;
private String password;
public static boolean userLogedIn;

public User(String username, String password) {
    this.userName = username;
    this.password = password;
}

public static void validateLogIn( LinkedList<User> users) {

    userLogedIn = false;

    String usernameToCheck, passwordToCheck;
    System.out.println("Enter your username: ");
    Scanner scanner = new Scanner(System.in);
    usernameToCheck = scanner.nextLine();

    System.out.println("Enter your password: ");
    passwordToCheck = scanner.nextLine();

    Iterator<User> spin = users.iterator();
    while (spin.hasNext()){
        User user = spin.next();

        //System.out.println("Searching");
        //System.out.println(username);
        String userToCheck
        if(user.userName.equals(userToCheck) && user.password.equals(passwordToCheck)) {
            //System.out.println("BINGO!");
            userLogedIn=true;
        } else {
            //return false;
            //System.out.println("User not found" + "\n");
        }
    }
}
私有字符串用户名;
私有字符串密码;
公共静态布尔userLogedIn;
公共用户(字符串用户名、字符串密码){
this.userName=用户名;
this.password=密码;
}
公共静态void validateLogIn(LinkedList用户){
userLogedIn=false;
字符串usernameToCheck,passwordToCheck;
System.out.println(“输入您的用户名:”);
扫描仪=新的扫描仪(System.in);
usernameToCheck=scanner.nextLine();
System.out.println(“输入密码:”);
passwordToCheck=scanner.nextLine();
迭代器spin=users.Iterator();
while(spin.hasNext()){
User=spin.next();
//System.out.println(“搜索”);
//System.out.println(用户名);
字符串userToCheck
if(user.userName.equals(userToCheck)和&user.password.equals(passwordToCheck)){
//System.out.println(“宾果!”);
userLogedIn=true;
}否则{
//返回false;
//System.out.println(“未找到用户”+“\n”);
}
}
}

if(username.equals(user.setUsername())和&password.equals(user.setPassword()))
想想这一行要做什么验证,对吗?如果当前迭代中的用户名等于用户设置的用户名,与密码相同,则会出现x。对吗?感谢您的帮助。我正在尝试从用户名和密码所在的链接列表中获取用户名和密码进行验证,并检查它们是否等于用户从控制台输入的用户名和密码。这正是此代码所做的,它从控制台(通过扫描仪)获取用户名和密码,并对照LinkedList中的每个记录进行检查