Question

In: Computer Science

code a Java program that: Requests from the user for a text filename to read the...

code a Java program that:

Requests from the user for a text filename to read the text inside, then it calculates the frequency of each letter contained in the file. When punctuation is ignored, there is no need to distinguish between upper and lower case. The text file should be named as “input” with only letters inside. For example,

a 11

c 9

e 16

Solutions

Expert Solution

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FileFrequency {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a file name: ");
        File file = new File(in.nextLine());    // read a file and create a file.
        try {
            Scanner fin = new Scanner(file);    // create a scanner to read from file
            int[] counts = new int[26];
            char ch;
            String line;
            while (fin.hasNextLine()) { // read each line from file
                line = fin.nextLine();
                for (int i = 0; i < line.length(); i++) {
                    ch = Character.toLowerCase(line.charAt(i));
                    if (Character.isAlphabetic(ch)) {
                        counts[ch-'a']++;
                    }
                }
            }
            for (int i = 0; i < counts.length; i++) {
                ch = (char) ('a' + i);
                System.out.println(ch + " " + counts[i]);
            }
            fin.close();    // close file.
        } catch (FileNotFoundException e) {
            System.out.println("Error. " + file.getAbsolutePath() + " does not exists!");   // if file does not exist, report an error
        }
    }
}

Related Solutions

Write a program that will ask for the user to input a filename of a text...
Write a program that will ask for the user to input a filename of a text file that contains an unknown number of integers. And also an output filename to display results. You will read all of the integers from the input file, and store them in an array. (You may need to read all the values in the file once just to get the total count) Using this array you will find the max number, min number, average value,...
in java (just a line of code will do) Your program will read in the text...
in java (just a line of code will do) Your program will read in the text file, word by word. You are to ignore leading and trailing punctuation and capitalization (i.e., "Castle", "castle?", and "CASTLE" are all equivalent to castle). Convert all words to lower case on input, or you can use case-insensitive comparisons - your choice. You will be putting the words into several implementations of linked lists and comparing the results. If, after trimming punctuation, a "word" consists...
Write Java program Lab52.java which reads in a line of text from the user. The text...
Write Java program Lab52.java which reads in a line of text from the user. The text should be passed into the method: public static String[] divideText(String input) The "divideText" method returns an array of 2 Strings, one with the even number characters in the original string and one with the odd number characters from the original string. The program should then print out the returned strings.
Ask the user for a filename, then ask the user for a line of text. Write...
Ask the user for a filename, then ask the user for a line of text. Write the line of text the filename specified by the user. The filename may be absolute or relative to the project directory.
Python code def plot_dataset(file_path): """ Read in a text file where the path and filename is...
Python code def plot_dataset(file_path): """ Read in a text file where the path and filename is given by the input parameter file_path There are 4 columns in the text dataset that are separated by colons ":". c1:c2:c3:c4 Plot 3 datasets. (x axis vs y axis) c1 vs c2 (Legend label "n=1") c1 vs c3 (Legend label "n=1") c1 vs c4 (Legend label "n=1") Make sure you have proper x and y labels and a title. The x label should be...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
2. Write a Java program to read a string (a password)from the user and then   check...
2. Write a Java program to read a string (a password)from the user and then   check that the password conforms to the corporate password policy.   The policy is:   1) the password must be at least 8 characters   2) the password must contain at least two upper case letters   3) the password must contain at least one digit   4) the password cannot begin with a digit   Use a for loop to step through the string.   Output “Password OK” if the password...
In java, ask the user for a filename. Display the oldest car for every manufacturer from...
In java, ask the user for a filename. Display the oldest car for every manufacturer from that file. Files Given: car-list.txt car-list-1.txt car-list-2.txt car-list-3.txt (they are too big for question) Required Output: Standard Input                 Files in the same directory car-list-1.txt car-list.txt car-list-1.txt car-list-2.txt car-list-3.txt Enter filename\n Oldest cars by make\n Acura Legend 1989 2T1BPRHE8EC858192\n Audi 80/90 1988 2GKALMEK4E6424961\n Bentley Continental Flying Spur 2006 WBA3G7C5XFK430850\n BMW 600 1959 1N6AA0ED2FN639969\n Bugatti Veyron 2009 SCFEBBBC5AG474636\n Buick Century 1987 2HNYD18625H455550\n Cadillac DeVille 1994...
Write an assembly code in MIPS program that can read 3 numbers from the user and...
Write an assembly code in MIPS program that can read 3 numbers from the user and print the following: a. The summation b. The average c. The minimum d. The maximum e. Print the values between the minimum and maximum f. Write comments to explain each line in your code -look at Fibonacci code- (no comments mean zero for the assignment ) use MARS MIPS .
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text label and a button.When the program begins, the label displays a 0. Then each time the button is clicked, the number increases its value by 1; that is each time the user clicks the button the label displays 1,2,3,4............and so on.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT