gpt4 book ai didi

android - 搜索文本文件的不确定性

转载 作者:行者123 更新时间:2023-11-29 21:46:24 25 4
gpt4 key购买 nike

friend ,我有一段代码可以读取文本文件并搜索匹配的词,但是在搜索文本文件时存在不确定性。有时它能够匹配单词,有时却不能匹配单词,尽管该单词存在于文本文件中。

代码如下:

EditText et = (EditText) findViewById(R.id.editText1);
String display = null;
String search = "dadas";
//String current = "woot";

try {
InputStream is = getAssets().open("file.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(is));

while(br.readLine() != null){

if(search.equalsIgnoreCase(br.readLine())){
display = "found";
break;
}

}

br.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

et.setText(display);

这是我的文本文件内容:

dadaist
dadaists
dadas
daddies
daddle
daddled
daddles
daddling

谁能弄清楚为什么会这样?假设我在我的文本文件中添加一个词“完成”,然后搜索它,它总会找到它。但是,如果我的搜索词是“dadas”或“dadist”,它会在 et 中产生 null。

最佳答案

您调用了 br.readLine() 两次,这意味着您将跳过所有其他行。在您的例子中,“dadas”在第 3 行,这意味着它被跳过了。

尝试:

    String line = br.readLine();
while(line != null){

if(line.equalsIgnoreCase(search)){
display = "found";
break;
}
line = br.readLine();
}

关于android - 搜索文本文件的不确定性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15693604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com