Question

In: Computer Science

In Java: Write a program that will count the number of characters, words, and lines in...

In Java:

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

Solutions

Expert Solution

Please find the code for the following:

Code:

import java.io.*;
import java.util.*;

class Exercise_12_13 {
   /** Main method */
   public static void main(String[] args) throws Exception
{
      
//Creating an object of Class File by calling the constructor
//With a parameter named args[0] which is th file name.
       File file = new File(args[0]);
      
       int characters = 0, words = 0, lines = 0;

//Here, we are using Java Scanner class for opening and reading a file.

           // Create an object of Scanner class by calling the constructor with the file parameter which we created
           Scanner s1 = new Scanner(file);
Scanner s2 = new Scanner(file);

//Iterating over all the lines and calculating the number of lines as well as the characters here.
           while (s1.hasNext())
{
               lines++; //Increment the line count
               String line = s1.nextLine();
               characters += line.length(); //Increment the characters count
           }
          
//Iterating over all the lines and then calculating the number of wods present in entire file.
           while (s2.hasNext())
{
               String line = s2.next();
               words++;
           }

       //Printing the result as provided in the sample output
       System.out.println("File " + args[0] + " has");
       System.out.println(characters + " characters");
       System.out.println(words + " words");
       System.out.println(lines + " lines");
   }
}

Please check the compiled program and its output for your reference:

Output:
Since the output of the file Loan.java varies from your file Loan.java. The result may vary but still is appropriate to the file which is being taken as a command line parameter for this class. please notice that.[I also outputted the Loan.java file for your understanding]

(I believe that I made the code simple and understandable. If you still have any query, Feel free to drop me a comment)


Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...


Related Solutions

Write a program that reads in characters until end of file. The program should count and...
Write a program that reads in characters until end of file. The program should count and print the number of characters, printable characters, vowels, digits, and consonants in the input. Use functions to check whether a character is a vowel, a consonant, or a printable character. Define and use macros to test if a character is a digit or a letter.
Java question- Write a java program to process the number.txt file. Then count the numbers and...
Java question- Write a java program to process the number.txt file. Then count the numbers and calculate the total average, even numbers average, odd number average, then print the corresponding information. The result should be displayed in the following format there are XX numebers in the file there are xx even numbers there are xx odd numbers the total number average is xx the odd number average is xx the even number average is xx I am having trouble using...
Write a Java program that will test lines of text to see if they are palindromes....
Write a Java program that will test lines of text to see if they are palindromes. The program should prompt the user for the name of a file to process, then open and read the file of possible palindromes (one per line of text). The program should then print the total number of lines read, and total number of palindromes.
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10. ###Note: the output should print exactly as it is stated in the example if...
2.c++ if and loop statement Write a program that will count the number of even number...
2.c++ if and loop statement Write a program that will count the number of even number and odd numbers between two inputted numbers. Display the numbers and compute the sum and average of all the even numbers and the sum and average all the odd numbers. Sample outputs: Enter starting number:3 Enter starting number:4 Enter ending number:10 Enter ending number:10 odd numbers Even number 3 4 5 6 7 8 9 10 number of even numbers=4 number of even numbers=4...
Write a program to reverse the lines of a file and to reverse the words plus...
Write a program to reverse the lines of a file and to reverse the words plus the letter's of each word within each line using ArrayList. A file name mobydick.txt Example: Original file contains the following MOBY DICK; OR THE WHALE by Herman Melville CHAPTER 1 Loomings. Out put should be .sgnimooL 1 RETPAHC ellivleM namreH yb ELAHW EHT RO ;KCID YBOM its for java eclipse
Java Question 5: Count elements in the heap Write a function that returns the number of...
Java Question 5: Count elements in the heap Write a function that returns the number of elements in a min heap strictly less than a given number. Method signature: public static int elemNumHeap(PriorityQueue minHeap, int val) Please also include testers.
JAVA Write a program that checks the spelling of words in a document. This program uses...
JAVA Write a program that checks the spelling of words in a document. This program uses two text files: A dictionary file containing all known correctly spelled words, and A document to be spell-checked against the dictionary. As the document to be spell checked is read, each of its words is checked against the dictionary words. The program determines whether each word is misspelled. Misspelled words are recorded. is spelled correctly. Correctly spelled words are recorded and their frequency counted....
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT