我想比较编辑文本的值与文件中的所有行(该文件是单词列表)。从文本文件中读取值并与edittext进行比较
我已经做到了这一点,
try{
final InputStream file = getAssets().open("words.txt");
reader = new BufferedReader(new InputStreamReader(file));
String line = reader.readLine();
while(line != null){
line = reader.readLine();
if(ss == line.toLowerCase()){
Toast.makeText(this, "Working!", Toast.LENGTH_SHORT)
.show();
}
else{
Toast.makeText(this, "Not found !", Toast.LENGTH_SHORT)
.show();
}
}
} catch(IOException ioe){
ioe.printStackTrace();
}
这里ss是文本框的值。
String ss = (res.getText()).toString();
这是words.txt文件 https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt
但上面的代码是行不通的。
编辑:
我检查了文件是否被打开与否,这个问题是文件没有打开。
try{
final InputStream file = getAssets().open("words.txt");
Toast.makeText(this, "File Opened", Toast.LENGTH_SHORT)
.show();
reader = new BufferedReader(new InputStreamReader(file));
String line ;
while((line = reader.readLine()) != null){
if(ss.equalsIgnoreCase(line)){
Toast.makeText(this, "Working!", Toast.LENGTH_SHORT)
.show();
TextView tv = myTextViewList.get(counter);
tv.setText(line);
}
else{
Toast.makeText(this, "Not found !", Toast.LENGTH_SHORT)
.show();
}
}
} catch(IOException ioe){
ioe.printStackTrace();
}
你是什么意思'它不工作'?不循环?它没有正确比较它们吗? – Ben
我的意思是那些敬酒没有显示。我有2敬酒如果找到这个词和没有匹配的词 –
给与Passiondroid的答案去吧,我认为应该工作 – Ben