Android - 比较文本文件中的字符串输入内容
我可以请您指导我如何完成此问题? 我需要将inputWord与.txt文件中的字符串进行比较,如果找到,则返回整行,但如果没有,则显示“找不到字”。Android - 比较文本文件中的字符串输入内容
例子:
inputWord: abacus
Text file content:
abaca - n. large herbaceous Asian plant of the banana family.
aback - adv. archaic towards or situated to the rear.
abacus - n. a frame with rows of wires or grooves along which beads are slid, used for calculating.
...
so on
Returns: abacus with its definition
什么,我要做的是在之前我inputWord比较的话“ - ”(连字符作为分隔符),如果他们不匹配,移动到下一行。如果它们匹配,则复制整行。
我希望它不像我问你“做我的作业”,但我尝试了围绕不同的论坛和网站的教程。我也阅读java文档,但我真的不能把它们放在一起来完成这一点。
预先感谢您!
UPDATE:
这里是我当前的代码:
if(enhancedStem.startsWith("a"))
{
InputStream inputStream = getResources().openRawResource(R.raw.definitiona);
try {
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String s = in.readLine();
String delimiter = " - ";
String del[];
while(s != null)
{
s = in.readLine();
del = s.split(delimiter);
if (enhancedStem.equals(del[0]))
{
in.close();
databaseOutput.setText(s);
break;
}
}
in.close();
}
catch (FileNotFoundException e) {
databaseOutput.setText("" + e);
}
catch (IOException e1) {
databaseOutput.setText("" + e1);
}
}
非常感谢!这是我想出来的,并且它正确地返回输入的定义,但问题是,当我输入文本文件中找不到的单词时,应用程序崩溃。口头禅似乎不起作用。有什么想法我可以陷入它? logcat中说,在4342线,其NullPointerExcepetion是
s = in.readLine();
假设在文本文件中的每一行的格式是统一的。这可以通过以下方式完成:
1)逐行读取文件。
2)根据分隔符分割每一行,并在temp字符串数组中收集拆分字符串标记。
3)temp标记数组中的第一项将是“ - ”号之前的单词。
4)将temp数组中的第一项与搜索字符串进行比较,如果匹配,则返回整行。
下面的代码可以在一个功能被竖起来实现这一点:
String delimiter = "-";
String[] temp;
String searchString = "abacus";
BufferedReader in = new BufferedReader(new FileReader(file));
while (in.readLine() != null) {
String s = in.readLine();
temp = s.split(delimiter);
if(searchString.equals(temp[0])) {
in.close();
return s;
}
}
in.close();
return ("Word not found");
希望这有助于。
,你可以尝试这样的:
myreader = new BufferedReader(new FileReader(file));
String text = "MyInput Word";
while(!((text.equals(reader.readLine())).equals("0")));
嗨!谢谢。我会尝试如果这个工作很快,我回家。 – 2013-02-26 04:51:42
我一直有问题filereader的路径。即使我输入了我的文本文件的确切路径,它也找不到我的目录。 :( – 2013-02-26 14:32:17
嗨!非常感谢你的回复。我会尽快回到这个代码:) – 2013-02-26 04:38:20
我得到一个错误,说:java.io.FileNotFoundException::打开失败:ENOENT(no这样的文件或目录) 我试图把这些到位 “文件” 的: “C:\\ \\工作区原\\ \\ definitiona.txt” 和 “\\ \\资源\\原料definitiona。 txt“ 但都表示找不到。 –
2013-02-26 04:58:44
尝试从“C:\\ workspace \\ raw \\ definitiona”中删除多余的\\。TXT \\”应该工作。 – 2013-02-26 05:29:16