我一直在尝试将文件内容与用户输入进行比较。程序正在读取特定文件,并检查用户的字符串输入。我无法将ArrayList与用户输入进行比较。
public class btnLoginListener实现了Listener {
@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();
}
答案 0 :(得分:0)
我假设您正在尝试检查exists
中的元素list
。如果是,那么您需要使用contains
方法,here's Javadoc。
因此,您可以使用if (username.equals(names))
。
if (names.contains(username))
除此之外,您还应进行以下更改:
ArrayList
。username
和password
设为本地。writeFile()
来电,除非它在每个事件上附加/写入动态值。