0

Im trying to create a project with a scanner to read a text file, separate by the number of lines, and also count the number of words in each line. Here is my code so far:

    public void getWordsPerLine(){

      try {
           File file = new File("report.txt");
            Scanner scanner = new Scanner(file);
            int count = 0;


            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                count++;


                if (count <= 9){
                System.out.println("");
                System.out.println("Line Number: " + count);
                System.out.println(line);
            }


            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


  }
4

3 に答える 3