我正在尝试为我的应用创建一个黑名单,因此用户通过editText输入的blacklist.txt文件中存在的任何单词都将被标记出来。我下面的代码没有做任何事情,我不知道为什么。
private String comparevalue() {
BufferedReader buffered = null;
String val = editbox.getText().toString();
InputStream inputStream =getResources().openRawResource(R.raw.blacklist);
buffered = new BufferedReader(new InputStreamReader(inputStream));
try {
String line = buffered.readLine();
while (line != null) {
line = buffered.readLine();
}if (val.equalsIgnoreCase(line)) {
Toast.makeText(this, "Working", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this,"Not found",Toast.LENGTH_LONG).show();
**}
return buffered.toString();
}**
答案 0 :(得分:1)
你把if放在了外面。所以你只是将val与null进行比较。
把它放在里面,在buffered.readLine()
之前,所以你不要忽略第一行。像这样:
line = buffered.readLine();
while (line != null)
{
if (val.equalsIgnoreCase(line))
{
Toast.makeText(this, "Working", Toast.LENGTH_LONG).show();
}
line = buffered.readLine();
}
答案 1 :(得分:0)
像这样更改您的代码
while (line != null) {
line = buffered.readLine();
if (val.equalsIgnoreCase(line)) {
Toast.makeText(this, "Working", Toast.LENGTH_LONG).show();
}
}