Parts of the code written: string moviename; getline(file, moviename); while (!f
ID: 3602774 • Letter: P
Question
Parts of the code written:
string moviename; getline(file, moviename);
while (!file.eof()) { . . . getline(file, moviename); }
Write the complete program to get the output in the picture. The lab exercise is a simple one: input movie names from a file, and output the total # of movies, the first movie, and the last movie. The input file contains the movie names, one per linethere is nothing special at the end. Example: suppose the file "movies.txt" contains The Shawshank Redemption (1994) The Godfather (1972) The Godfather: Part II (1974) Pulp Fiction (1994) The Good the Bad and the Ugly (1966) The Dark Knight (2008) 12 Angry Men (1957) Schindler's List (1993) The Lord of the Rings: The Return of the King (2003) Fight Club (1999) Your program should input the name of the file from the keyboard: movies.txt The output from your program should be the # of movies, first movie name, and last movie name: 10 The Shawshank Redemption (1994) Fight Club (1999)
Explanation / Answer
Hello did not get the last line its taking input of file counting the lines and Displaying the contents
package cheggtask;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.Scanner;
public class Filemovie {
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
System.out.println("Enter filepath");
String s=sc.nextLine();
BufferedReader br=null;
br=new BufferedReader(new FileReader(s));//storing the file path into buffered reader
String txtFile = s;
String linen= "";
String txtSplitBy = " "; // taking each word by a split
br = new BufferedReader(new FileReader(txtFile));
FileReader fr=new FileReader(txtFile); //reading the file line by line
LineNumberReader l= new LineNumberReader(fr);//using line numbe reader to read each line
int line = 0; //starts the index from 0
while (lnr.readLine() != null){
line++;
}
System.out.println(line);//displaying count of no of lines in file
l.close();
while ((linen = br.readLine()) != null) {
String[] movies= linen.split(txtSplitBy); //entering the list into an array
System.out.println( movies[0] + " ");
}
}
}