Question

Assume a text file called film_reviews.txt exists with the following contents: 7 Wall-E 5 The Martian...

Assume a text file called film_reviews.txt exists with the following contents:

7

Wall-E

5

The Martian

4

Wall-E

4

The Intern

3

...

The first line of the input file shows the number of reviews in the file. Afterward, each film/review p0ari is then written in subsequent lines in the file. Write a Java application which will read the contents of film_reviews.txt and produce an output file named review_averages.txt which contains the film name and the average of reviews in the file. The output file could look like the following:

Wall-e 4.3333

The Martian 5.0000

The Intern 3.6666

JAVA File

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

import java.util.ArrayList;

class Movie{
        String name;
        ArrayList<Integer> ratings;
        Movie(String n){
                name = n;
                ratings = new ArrayList<Integer>();
        }
        public double getAverage() {
                double avg = 0;
                double sum = 0;
                int n = ratings.size();
                for(int i=0; i<n; i++) {
                        sum += this.ratings.get(i);
                }
                avg = sum/n;
                return avg;
        }
}

package HomeworkLib1;
import java.io.*;
import java.util.ArrayList;
public class FilmReview {

        
        public static void main(String[] args) {
                int n = 0;                                                      //No of movies
                ArrayList<Movie> movies = new ArrayList<Movie>();   //List of movies
                try {
                        File myfile = new File("src/HomeworkLib1/review.txt");
                        FileReader filereader = new FileReader(myfile);
                        BufferedReader reader = new BufferedReader(filereader);
                        n = Integer.parseInt(reader.readLine());                        //First line
                        
                        String line = "";
                        
                        while((line=reader.readLine())!=null) {
                                Movie m = new Movie(line);                              //Create temporary movie
                                String str = reader.readLine();                 //Get rating from file
                                Integer x = Integer.parseInt(str);
                                m.ratings.add(x);                                               //Set rating
                                //Check if a movie with this name already exists in the list
                                int i;
                                for(i=0; i<movies.size(); i++) {
                                        //line contains movie name, if it's a match, break
                                        //i contains the index of that movie
                                        if(movies.get(i).name.contentEquals(line))
                                                break;
                                }
                                if(i<movies.size()) {                                        //Match was found in the list
                                        movies.get(i).ratings.add(x);           //Add rating
                                }else {                                                                 //Match was not found
                                        movies.add(m);
                                }
                        }
                        reader.close();
                }catch(IOException e) {
                        e.printStackTrace();
                }
                try {
                        File myFile = new File("src/HomeworkLib1/review_averages.txt");
                        FileWriter fileWriter = new FileWriter(myFile);
                        BufferedWriter writer = new BufferedWriter(fileWriter);
                        
                        int i = 0;
                        for(i=0; i<movies.size(); i++) {
                                String name = movies.get(i).name;
                                double average = movies.get(i).getAverage();
                                writer.write(name + " " + average + "\n");
                        }
                        writer.close();
                }catch(IOException e) {
                        e.printStackTrace();
                }
        }
}

Add a comment
Know the answer?
Add Answer to:
Assume a text file called film_reviews.txt exists with the following contents: 7 Wall-E 5 The Martian...
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
  • 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...

  • Create and save a new text file called Gasoline that consists of the following content: Gas prices stayed about the sa...

    Create and save a new text file called Gasoline that consists of the following content: Gas prices stayed about the same the past two weeks, continuing an unusual 12 week trend of steadily increasing prices followed by prices relaxing. The average price for gas nationwide, including all grades and taxes, was about $2.61 a gallon on Friday, according to a survey of 8,000 stations released Sunday. That was up .06 cents per gallon from the prior survey. Prices have been...

  • please answer it in a java console application form (java GUI). Least Square Lines Equation- Text File /O Suppose we ha...

    please answer it in a java console application form (java GUI). Least Square Lines Equation- Text File /O Suppose we have a text file (which I supplied named data.txt) that has the following table: Temperature (celsius) Resistance (ohms) 20.0 761 31.5 817 50.0 874 71.8 917 91.3 1018 Write a Java console applicatlon, that does the following: 1. Prompt the user for the name of the text file 2. Opens the text file and reads in the ordered pair data...

  • Assume you have the following text file: players.txt Pikachu,10 Geodude,50 Zubat,75 Marowak,37 Each line contains a...

    Assume you have the following text file: players.txt Pikachu,10 Geodude,50 Zubat,75 Marowak,37 Each line contains a player's name and starting health, separated by a comma (,). Write a method called createPlayers which reads the lines from that file and returns an array of Player objects matching the properties in the input file. Assume you have access to the Inclass we've been using to read text from an input file, and that it has been correctly imported at the top of...

  • What to submit: your answers to exercises 2. Write a Java program to perform the following...

    What to submit: your answers to exercises 2. Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (if the input file does not exist it should throw an exception) and read the contents line by line. It should also open the output file as a text file and write...

  • 1. Create an RSA private key 2. Output the key in a text format so that it shows the following:     modulus     public e...

    1. Create an RSA private key 2. Output the key in a text format so that it shows the following:     modulus     public exponent (e)     private exponent (d)     the primes (p and q) Send me a file called key.txt with this information. 3. Using openssl's rsautl option encrypt this message to me: "NAME" using the public key that's embedded with the private key above. Attach a file named encrypted.txt that contains the encrypted message. Hint: Copy the text above and put...

  • Write the following Java code for a file called input.txt that has the below text in...

    Write the following Java code for a file called input.txt that has the below text in it. 5 6 1 1 0 0 1 1 0 1 0 0 1 1 0 1 0 0 2 1 0 3 1 1 1 1 1 2 3 4 5 1 2 0 1 hello 2 3 bye Create the Entry class. · Entry should have 3 data members o int x. o int y. o String name. Create a class called...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • *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....

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