Question

In JAVA

Twenty students were asked to rate on a scale of 1 to 5 the quality of the food in the student cafeteria, with 1 being awful and 5 being excellent. The 20 responses are stored in numbers.txt file. Read the responses from numbers.txt file, determine the frequency of each rating and output it to the text file output.txt The responses should be read from the file by using a Scanner. Use method nextint to input one integer at a time from the file. The program should continue to read responses until it reaches the end ot the tile. The results should be output to the text tile output.txt.Use a Formatter to create a file called output.tx. Each integer should be wrtten using method format. Sample input and output are shown in the below table. numbers.txt 31253322332143144533Rating Frequency output.txt

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for posting the question, we are glad to help you .Here is the complete code in Java. Below is the output i generated in my system. You may need to change the below two paths in your system

private static final String inputFilePath = "F:\\numbers.txt";
private static final String outputFilePath = "F:\\output.txt";

____________________________________________________________________________________________________

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

public class Ratings {

    private static final String inputFilePath = "F:\\numbers.txt";
    private static final String outputFilePath = "F:\\output.txt";
    private static java.util.Scanner console;

    public static void main(String[] args) {

        int[][] ratingCount = new int[5][1];
        try {
            console = new java.util.Scanner(new File(inputFilePath));
            while (console.hasNextLine()) {
                String line = console.nextLine();
                String[] ratings = line.split("\\s+");
                for (String rating : ratings) {
                    if (rating.trim().length() == 0)
                        continue;
                    int rate = Integer.parseInt(rating);
                    if (rate == 1)
                        ratingCount[0][0] = 1 + ratingCount[0][0];
                    else if (rate == 2)
                        ratingCount[1][0] = 1 + ratingCount[1][0];
                    else if (rate == 3)
                        ratingCount[2][0] = 1 + ratingCount[2][0];
                    else if (rate == 4)
                        ratingCount[3][0] = 1 + ratingCount[3][0];
                    else if (rate == 5)
                        ratingCount[4][0] = 1 + ratingCount[4][0];
                }
            }
            console.close();


        } catch (FileNotFoundException fne) {
        }
        writeToFile(ratingCount);
    }

    public static void writeToFile(int[][] ratingCount) {
        Formatter output = null;
        try {
            output = new Formatter(outputFilePath);
            output.format("%-10s%-10s\r\n", "Rating", "Frequency");
            for (int index = 0; index < ratingCount.length; index++) {
                output.format("%-12d%-12d\r\n", index + 1, ratingCount[index][0]);
            }
            output.flush();


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            output.close();
        }
    }
}

___________________________________________________________________________________________________

Thank you !

Add a comment
Know the answer?
Add Answer to:
In JAVA Twenty students were asked to rate on a scale of 1 to 5 the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA (Student Poll) Figure 7.8 contains an array of survey responses that’s hard coded into the...

    JAVA (Student Poll) Figure 7.8 contains an array of survey responses that’s hard coded into the program. Suppose we wish to process survey results that are stored in a file. This exercise requires two separate programs. First, create an application that prompts the user for survey responses and outputs each response to a file. Use a Formatter to create a file called numbers.txt. Each integer should be written using method format. Then modify the program in Fig. 7.8 to read...

  • JAVA Which of these statements does not match by using exception action in Java? 1. Exception...

    JAVA Which of these statements does not match by using exception action in Java? 1. Exception action makes it possible for the calling method to handle errors in called methods.    2. Exception action simplifies programming since incorrect reporting and handling can be located in catch blocks separately from the rest of the code.    3. Exception action increases the performance of programs. 4. Java separates exception action from common processes. Why create instances of File Class? - several alternatives...

  • Need help with java programming. Here is what I need to do: Write a Java program...

    Need help with java programming. Here is what I need to do: Write a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a “$integer”, will replace that variable by a corresponding value in a 2ndfile, which will be called the “variable value file”. The requirements for the assignment: 1.     The input and variable value file are both text files that will be specified in...

  • Write a Java program in Eclipse that reads from a file, does some clean up, and...

    Write a Java program in Eclipse that reads from a file, does some clean up, and then writes the “cleaned” data to an output file. Create a class called FoodItem. This class should have the following: A field for the item’s description. A field for the item’s price. A field for the item’s expiration date. A constructor to initialize the item’s fields to specified values. Getters (Accessors) for each field. This class should implement the Comparable interface so that food...

  • Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written...

    Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...

  • (Java) Rewrite the following exercise below to read inputs from a file and write the output...

    (Java) Rewrite the following exercise below to read inputs from a file and write the output of your program in a text file. Ask the user to enter the input filename. Use try-catch when reading the file. Ask the user to enter a text file name to write the output in it. You may use the try-with-resources syntax. An example to get an idea but you need to have your own design: try ( // Create input files Scanner input...

  • *Java* You will write a program to do the following: 1. Read an input file with...

    *Java* You will write a program to do the following: 1. Read an input file with a single line of text. Create a method named load_data. 2. Determine the frequency distribution of every symbol in the line of text. Create a method named 3. Construct the Huffman tree. 4. Create a mapping between every symbol and its corresponding Huffman code. 5. Encode the line of text using the corresponding code for every symbol. 6. Display the results on the screen....

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • The following code uses a Scanner object to read a text file called dogYears.txt. Notice that...

    The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this: Tippy 2 Rex 7 Desdemona 5 1. Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will...

  • Lab 19 - History of Computer Science, A File IO Lab FIRST PART OF CODING LAB:...

    Lab 19 - History of Computer Science, A File IO Lab FIRST PART OF CODING LAB: Step 0 - Getting Starting In this program, we have two classes, FileIO.java and FileReader.java. FileIO.java will be our “driver” program or the main program that will bring the file reading and analysis together. FileReader.java will create the methods we use in FileIO.java to simply read in our file. Main in FileIO For this lab, main() is provided for you in FileIO.java and contains...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT