Question

In: Computer Science

Write a Java program that allows the user to specify a file name on the command...

Write a Java program that allows the user to specify a file name on the command line and prints the number of characters, words, lines, average number of words per line, and average number of characters per word in that file. If the user does not specify any file name, then prompt the user for the name.

Solutions

Expert Solution

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class FileStats {
        public static void main(String[] args) throws Exception {
                String name = "";
                if (args == null || args.length == 0) {
                        Scanner sc = new Scanner(System.in);
                        System.out.println("Enter filename: ");
                        name = sc.next();

                } else {
                        name = args[0];
                }

                File file = new File(name);
                BufferedReader reader = new BufferedReader(new FileReader(file));

                String line;

                // Initializing counters
                int countWord = 0;
                int lineCount = 0;
                int charCount = 0;

                // Reading line by line from the
                // file until a null is returned
                while ((line = reader.readLine()) != null) {
                        if (!(line.equals(""))) {

                                String[] wordList = line.split(" ");
                                lineCount++;
                                countWord += wordList.length;
                                charCount = charCount + line.length();
                        }
                }

                System.out.println("Line count: " + lineCount);
                System.out.println("Word count: " + countWord);
                System.out.println("Char count: " + charCount);
                System.out.println("Avereage words per line: " + (countWord/(double)lineCount));
                System.out.println("Avereage chars per wod: " + (charCount/(double)countWord));
                
                
                reader.close();

        }
}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME


Related Solutions

Write a program that allows the user to navigate the lines of text in a file....
Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...
Write a program that allows the user to navigate the lines of text in a file....
Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Write a program that asks the user for a file name. The file contains a series...
Write a program that asks the user for a file name. The file contains a series of scores(integers), each written on a separate line. The program should read the contents of the file into an array and then display the following content: 1) The scores in rows of 10 scores and in sorted in descending order. 2) The lowest score in the array 3) The highest score in the array 4) The total number of scores in the array 5)...
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
Java Programming: Write a program that allows the user to compute the power of a number...
Java Programming: Write a program that allows the user to compute the power of a number or the product of two numbers. Your program should prompt the user for the following information: • Type of operation to perform • Two numbers (the arguments) for the operation to perform The program then outputs the following information: • The result of the mathematical operation selected. Menu to be displayed for the user: MATH TOOL 1. Compute the power of a number 2....
Write a program that prompts the user for a file name, make sure the file exists...
Write a program that prompts the user for a file name, make sure the file exists and if it does reads through the file, count the number of times each word appears and then output the word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a file name Check that the file can be opened and if not ask the user to try again (hint:...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
Write a program called Assignment3 (saved in a file Assignment3 .java) that asks a user to...
Write a program called Assignment3 (saved in a file Assignment3 .java) that asks a user to enter two strings. First, the program prompts: Please enter a string. The program should read in the string, and prompts: Please enter another string. The program reads in two strings and it prints a menu to the user. The program asks for user to enter an option and performs one of the following: Here are the options on the menu: Option a: checks if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT