【发布时间】:2015-08-17 10:39:04
【问题描述】:
我想将编辑文本的值与文件中的所有行进行比较。(文件是单词列表)。
我已经做到了,
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 是 textfield 的值。
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();
}
【问题讨论】:
-
“它不工作”是什么意思?是不是循环播放?是不是比较正确?
-
我的意思是那些祝酒词没有显示。如果找到这个词并且没有匹配的词,我有 2 个祝酒词
-
试一试 Passiondroid 的回答,我认为应该可行
-
使用 .equals() 代替 ==
标签: android file-handling word-list