1.) Write a program that will read a text file that contains an unknown number o
ID: 3547939 • Letter: 1
Question
1.) Write a program that will read a text file that contains an unknown number of movie review scores. Read the scores as Double values and put them in an instance of ArrayList. Compute the average score.thanks in advance 1.) Write a program that will read a text file that contains an unknown number of movie review scores. Read the scores as Double values and put them in an instance of ArrayList. Compute the average score.
thanks in advance
Explanation / Answer
-----------------------------------------------------------file.txt-----------------------------------------------------
iron-man 4.0
terminator 7.9
terminator1 2.3
Turbo 6.7
-------------------------------------------------------------program-----------------------------------------------
package mypro;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
public class movie {
public static void main(String[] args) {
double sum=0.0;
FileReader r = null;
try {
r = new FileReader("file.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Scanner in =new Scanner(r);
ArrayList<Double> al = new ArrayList<Double>();
while(in.hasNext()){
//i++;
String movie = in.next();
al.add(in.nextDouble());
}
for(double j :al){
sum= sum+ j;
}
System.out.println("Average Score of movies="+ sum/al.size());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.