搜索和计算文本文件中的单词,但无法忽略使用分隔符的完全停止

时间:2017-01-24 19:40:34

标签: javascript java java.util.scanner delimiter

搜索并计算文本文件中的单词,但无法忽略使用分隔符的句号。我正在使用分隔符来忽略句号后出现的句号。我试图通过使用用户输入创建一个文件,然后询问他们想要在文本文件中搜索的单词,并向他们显示文本中出现的次数。     这是我的主要课程     公共课主     {

public static void main(String []args) throws IOException
{

       str s=new str();
        s.writer();
        s.reader();


}
}



this is my class that has both the methods to create and read a file.

public void reader()
{
      String s;
    int i=0;
    //Reading The File
        try {
            FileReader f=new FileReader("karan.txt");
              Scanner sc=new Scanner(f);
               Scanner st=new Scanner(System.in);
                System.out.print("Enter string to be found and counted: ");
                s=st.nextLine();
                System.out.print("\n");
                sc.useDelimiter(".");
                sc.nextLine();
              while(sc.hasNext())
              {
                  if(sc.next().equals(s))
              {

                  i++;
              }
              }
             System.out.println("The number of times the word "+s+" is present is "+i);

        } catch (FileNotFoundException ex) {
            System.out.println("File not found");
        }
}
 //Writing The File
public void writer() throws IOException
{

        try {
            FileWriter fw=new FileWriter("karan.txt");
              PrintWriter p=new PrintWriter(fw) ;
               Scanner st=new Scanner(System.in);
                System.out.println("Enter Text.");
                p.print(st.nextLine());
                p.close();
            System.out.println("File has been written successfuly.");

        } catch (FileNotFoundException ex) {
            System.out.println("Error!");
        }
}

这是我保留分隔线

时的输出
    Enter Text.
    karan is best of the best.
    File has been written successfuly.
    Enter string to be found and counted: best
    The number of times the word best is present is 0

这是删除分隔符行时的输出 输入文字。

    karan is best of the best
    File has been written successfuly.
    Enter string to be found and counted: best
    The number of times the word best is present is 2
    BUILD SUCCESSFUL (total time: 23 seconds)

但即使我写作,我希望答案是2         卡兰是最好的。 注意结束时的完整停止,我希望编译器忽略句号并给我答案2,即它也应该在最后用完全停止计算最佳单词。

感谢提前帮助。

1 个答案:

答案 0 :(得分:0)

您需要对reader()方法进行一些更改:

  • sc.nextLine();循环之前删除while,跳过第一行并将光标前进到下一行,如果文件只有一行,它将无法工作。

  • sc.useDelimiter(".");更改为sc.useDelimiter("\\.");,您需要转发.字符,useDelimiter接受正则表达式,.表示正则表达式中的任何字符。< / p>

  • if(sc.next().equals(s))更改为if(sc.next().contains(s)),您只需要检查该行中的string是否存在。

下面应该有效:

try {
    FileReader f = new FileReader("G://1.txt");
    Scanner sc = new Scanner(f);
    Scanner st = new Scanner(System.in);
    System.out.print("Enter string to be found and counted: ");
    s = st.nextLine();
    System.out.print("\n");
    sc.useDelimiter("\\.");
    while (sc.hasNext()) {
        if (sc.next().contains(s)) {

            i++;
        }
    }
    System.out.println("The number of times the word " + s + " is present is " + i);

} catch (FileNotFoundException ex) {
    System.out.println("File not found");
}