Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java FileStats类的问题_Java - Fatal编程技术网

Java FileStats类的问题

Java FileStats类的问题,java,Java,我在使用FileStats类来显示文件有多少行以及有多少行包含文本时遇到了一些问题。我已经能够成功地显示文件有多少行,但我不断得到错误的答案,有多少行包含文本 例如,如果一个文件包含5268行,其中1137行包含单词“the”,那么我的代码返回的输出表示它只包含553行包含单词“the” 任何帮助都将不胜感激,谢谢。代码如下 import java.util.Scanner; import java.io.BufferedReader; import java.io.File; import j

我在使用FileStats类来显示文件有多少行以及有多少行包含文本时遇到了一些问题。我已经能够成功地显示文件有多少行,但我不断得到错误的答案,有多少行包含文本

例如,如果一个文件包含5268行,其中1137行包含单词“the”,那么我的代码返回的输出表示它只包含553行包含单词“the”

任何帮助都将不胜感激,谢谢。代码如下

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Romeo

{
    public static void main(String[] args) throws Exception
    {
        Scanner input=new Scanner(System.in); //creating object for scanner class
        System.out.println("Enter a filename");
        String file_name=input.nextLine(); //asking user to enter a file name


        int word_line_count=0,line_count=0; //count variables for counting the line count and word occurance count
        String search_word=input.nextLine(); //asking user to enter a search_Word
        File f=new File(file_name); //Creating File Descriptor for reading input file
        String[] words_sentence=null; //creating string which stores the all words in a line
        FileReader file_object = new FileReader(file_name); // Creating File Reader object
        BufferedReader buffer = new BufferedReader(file_object); //Creating BufferedReader object
        String sentence;

        while((sentence=buffer.readLine())!=null) //Reading Content from the file till the end of file
        {
            words_sentence=sentence.split(" "); //Split the sentence by using space delimiter
            for (String word : words_sentence) //itearte for each loop for every word in sentence
            {
                if (word.equals(search_word))
                {// if word in the sentence equals to search word increment the count valie
                    word_line_count++;
                    break; //if one word is equal in the sentence break the loop because no need to check entire sentence
                }
            }
            line_count++; //increment line count for every while loop iteration
        }

        System.out.println(file_name + " has " + line_count + " lines"); //printing the line count

        System.out.println("Enter some text");

        System.out.println( word_line_count+ " line(s) contain" ); //printing the number of lines contains the given word
        file_object.close(); //closing file object
    }
}

别把事情弄得这么复杂。String类中有一个简单的方法可以帮助您更轻松地解决这个问题:contains()

请看下面的if语句

    public static void main(String[] args) throws Exception
{
    Scanner input = new Scanner(System.in); //creating object for scanner class
    System.out.println("Enter a filename");
    String file_name = input.nextLine(); //asking user to enter a file name


    int word_line_count =0, line_count=0; //count variables for counting the line count and word occurance count
    String search_word = input.nextLine(); //asking user to enter a search_Word
    File f = new File(file_name); //Creating File Descriptor for reading input file
    String[] words_sentence = null; //creating string which stores the all words in a line
    FileReader file_object = new FileReader(file_name); // Creating File Reader object
    BufferedReader buffer = new BufferedReader(file_object); //Creating BufferedReader object
    String sentence;

    while((sentence=buffer.readLine())!=null) //Reading Content from the file till the end of file
    {

            if (sentence.contains(search_word))
            {
                word_line_count++;
            }

        line_count++; //increment line count for every while loop iteration
    }

    System.out.println(file_name + " has " + line_count + " lines"); //printing the line count

    System.out.println("Enter some text");

    System.out.println( word_line_count+ " line(s) contain" ); //printing the number of lines contains the given word
    file_object.close(); //closing file object
}

将文件最小化为程序无法运行的单行。以防万一,您忽略标点符号(例如,您不会检测到。)和大写变体(
the
)。这样效果更好,但我相信我的代码忽略了标点符号(例如,它不会检测。)和大写变体,如(the)。