【问题标题】:dictionary in java using java.io使用 java.io 在 java 中的字典
【发布时间】: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


【解决方案1】:

//这段代码正在读取这样一个文件:

Hello - to greet
Circle - a round shape

//那么代码可以这样写,这样可以吗?

  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("/Folder/demo1.txt");
                in = new BufferedReader(new FileReader(f));
                String s;

                while((s = in.readLine()) != null){

                    int x = s.indexOf(str);
                 //   System.out.println("Index of dash:" + s.indexOf("-"));
                 //   System.out.println("Index of Hello:" + x);

                    if(x != -1 && x<s.indexOf("-")){

                        String sub = s.substring(0,s.indexOf("-"));
                        System.out.println("Sub:" + sub);
                        System.out.println("Str:" + str);
                        if(sub.trim().equals(str.trim())){
                              System.out.println("Success:" +sub);
                               notfound = true;
                               break;


                        }

                        else{
                            System.out.println("Word is not present") ;
                            notfound = false;
                            break;
                        }
                    }



                }
                if(!notfound){
                    System.out.println("Try another word?(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();
            }

        }
    }
}

【讨论】:

  • 我认为这不是问题所在。为了跳过一行,我使用了 readline() 两次
  • @Ayesha 是的,我确实运行了它,但我使用了“s = in.readLine();”在我把它贴在这里之前。我再次编辑了我的答案
  • @Ayesha 实际上我要取消这条线,因为它不能正常工作。另一种方法实际上是有效的,它只需要在成功找到单词时中断,除了循环到下一个单词。我编辑了答案并输入了休息时间
  • @Ayesha 我还拿出了我一直混淆的不必要的打印语句
猜你喜欢
  • 2022-01-01
  • 2011-04-17
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2014-06-12
  • 2013-08-19
相关资源
最近更新 更多