Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write the following program in Java. Verify it will run correctly in a Ja

ID: 3761761 • Letter: P

Question

Please write the following program in Java. Verify it will run correctly in a Java IDE. No Rush take your time.

(Count characters, words, and lines in a file) 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 in Figure 12.13.

Below is an example of the output. When the program is run it should open Command Prompt. From there I should be able to enter any file name on my computer. After the file name is type and I press Enter, I should see the following

Name of the file has

Number of characters

Number of words

Number of Lines

Example of Command Prompt and output:

Command Prompt -| c lexercise java Exercise12 13 Loan. java File Loan.java has 1919 characters 210 words 71 lines c:exercise> FIGuRE 12.13 file The program displays the number of characters, words, and lines in the given

Explanation / Answer

public class count {

public static void main(String[] args) throws Exception {
java.io.File file = new java.io.File(args[0]);
Scanner input = new Scanner(file);
int character=0;
int line=0;

int word=0;
while(input.hasNextLine())
{
String s=input.nextLine();
line++;
character +=s.length();
// or character +=s.length()-2;
}

input.close();
input = new Scanner(file);
int word = 0;
while (input.hasNext()){
input.next();
word++;
}
input.close();


input = new Scanner(file);
String scan = input.nextLine();
System.out.println(scan);
String scan2[] = "Testing 123 ".split(" ");


System.out.println("Number of characters = " + character);
System.out.println("Number of lines = " + line);
System.out.println("Number of words = " + word);


}
}