【发布时间】:2014-11-18 14:14:29
【问题描述】:
我在 java 中编写了字典代码,在其中我从名为 newFile.txt 的文件中读取数据。在文件中,世界位于一行,其含义位于下一行。用户进入一个世界。如果在文件中找到单词,则显示其含义放在下一行,如果未找到单词,则显示相似的单词(子字符串)。
“在搜索单词时,它不应该搜索含义。”
import java.io.*;
import java.util.*;
public class Notepad {
public static void main(String []args) throws IOException{
BufferedReader in = null;
Scanner input = new Scanner(System.in);
String str;
boolean notfound = false;
char again = 'a';
try{
do{
notfound = false;
System.out.println("Enter word :");
str = input.next();
File f = new File("D:\\newFile.txt");
in = new BufferedReader(new FileReader(f));
String s;
while((s = in.readLine()) != null){
int x = s.indexOf(str);
if(x != -1){
int lens = s.length();
String sub = s.substring(x);
int lensub = str.length();
if(lens == lensub){
System.out.println((in.readLine()));
break;
}
else{
System.out.println(sub) ;
notfound = true;
}
}
s = in.readLine();
}
if(!notfound){
System.out.println("Try another world?(y/n):");
again = input.next().trim().charAt(0);
again = Character.toLowerCase(again);
}
}
while(notfound || again == 'y');
System.out.println("terminated!");
}
finally{
if(in != null){
in.close();
}
}
}
}
当我输入一个单词的子字符串时,它也会搜索含义,然后如果输入正确的单词,它不会显示含义
【问题讨论】:
-
我没有字典来找出你问题的意思
-
我想问题是如何从单词搜索中过滤掉含义。如果是这样,通过构建您当前的解决方案,您可以保留行号的计数器。
-
当我输入一个单词的子字符串时,它也会搜索含义,然后如果输入正确的单词,它不会显示含义
-
我运行了你的代码,我想我明白发生了什么,你的 newFile.txt 是如何设置的。在我的文件中,我有“你好 - 一个问候语”之类的和你的相似吗?
-
一行单词,下一行单词含义
标签: java dictionary java-io