Question: 1. Write a method that reads a text file and, for each line, prints out the number of words in the line. Try it on

1. Write a method that reads a text file and, for each line, prints out the number of words in the line. Try it on the file story.txt. May i know how to do this? Here's the code i wrote.

package lab8;

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class LineNumberer

{

public static void main(String[] args) throws FileNotFoundException

{

File file = new File("Deck.java");

Scanner scanner = new Scanner(file);

int lineCount = 1;

while (scanner.hasNextLine())

{

String line = scanner.nextLine();

System.out.print(lineCount + " ");

System.out.println(line);

lineCount += 1;

}

scanner.close();

System.out.println("Done");

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!