Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.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 将ArrayList与用户输入进行比较_Java_User Interface_Arraylist_Compare_User Input - Fatal编程技术网

Java 将ArrayList与用户输入进行比较

Java 将ArrayList与用户输入进行比较,java,user-interface,arraylist,compare,user-input,Java,User Interface,Arraylist,Compare,User Input,我一直在尝试将文件内容与用户输入进行比较。程序正在读取特定文件,并根据用户的字符串输入进行检查。我无法将ArrayList与用户输入进行比较 公共类btnLoginListener实现了Listener { 我假设您正在尝试检查列表中是否存在元素。如果是,则需要使用contains方法Javadoc 因此,您可以使用if(names.contains(username)),而不是使用if(username.equals(names)) 除此之外,您还应进行以下更改: 不要每次调用事件时都读取该

我一直在尝试将文件内容与用户输入进行比较。程序正在读取特定文件,并根据用户的字符串输入进行检查。我无法将ArrayList与用户输入进行比较

公共类btnLoginListener实现了Listener {


我假设您正在尝试检查
列表中是否存在
元素。如果是,则需要使用
contains
方法Javadoc

因此,您可以使用
if(names.contains(username))
,而不是使用
if(username.equals(names))

除此之外,您还应进行以下更改:

  • 不要每次调用事件时都读取该文件。在读取静态文件时,可以读取一次并将其存储在
    ArrayList
  • 将变量
    用户名
    密码
    设为本地变量
  • 删除
    writeFile()
    调用,除非它在每个事件上追加/写入动态值

我无法将ArrayList与用户输入进行比较。
有点太模糊。请再解释一点,如果是例外,请共享堆栈跟踪。您可以解释名称的来源。名称的类型是什么?用户名的类型是什么?我怀疑列表和字符串。字符串怎么可能等于列表?是什么你想实现什么?你必须定义“比较”和“对照”。
    @Override
    public void handleEvent(Event arg0) 
    {

        //variables for the class
        username = txtUsername.getText();
        password = txtPassword.getText();

        MessageBox messageBox = new MessageBox(shell, SWT.OK);

        try {
            writeFile();
            messageBox.setMessage("Success Writing the File!");
        } catch (IOException x)
        {
            messageBox.setMessage("Something bad happened when writing the file!"); 
        }

        try {
            readFile("in.txt");

        } catch (IOException x)
        {
            messageBox.setMessage("Something bad happened when reading the file!" + x);
        }

        if (username.equals(names))
        {
            messageBox.setMessage("Correct");
        }
        else
        {
            messageBox.setMessage("Wrong");
        }       

        messageBox.open();
    }
}

private static void readFile(String fileName) throws IOException 
{
    //use . to get current directory
    File dir = new File(".");
    File fin = new File(dir.getCanonicalPath() + File.separator + fileName);

    // Construct BufferedReader from FileReader
    BufferedReader br = new BufferedReader(new FileReader(fin));


    String line = null;
    while ((line = br.readLine()) != null)
    {
        Collections.addAll(names, line);
    }       

    br.close();
}