Question: Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name

Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown below.

c:\exercise>java Exercise12_13 Loan.java

File loan.java has

1919 characters

210 words

71 lines

c:\exercise>

Class Name:

Exercise12_13

fix code:

import java.io.*; import java.util.*; public class Exercise12_13 { public static void main(String[] args) throws Exception { if(args.length != 1) { System.out.println("Command-line argument is missing!"); System.exit(1);; } String fileName = args[0]; File source = new File(fileName); if(!source.exists()) { System.out.println(fileName + "file does not exist!."); System.exit(2);; } Scanner infile = new Scanner(source); String line; int characterCount = 0; int wordsCount = 0; int linesCount = 0; while(infile.hasNextLine()) { line = infile.nextLine(); linesCount++; String[] words = line.split(""); wordsCount += words.length; for(String token : words) { characterCount+=token.length(); } } System.out.println("Name of the input file:" +fileName); System.out.println("Number of lines in the file:" +linesCount); System.out.println("Number of words in the file:" +wordsCount); System.out.println("Number of characters in the file:" +characterCount); } }

Remarks and hints

Checking output

Your standard output is not what was expected.

same code error as the above only java person.com approve

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!