I am having problems opening text files in the same folder as my java files. The program is returning the message "File not found!" as specific in TestMovies.java:
try { sc = new Scanner(new File("movies.txt")); } catch (FileNotFoundException e) { System.out.println("File not found!"); return; }
and
try { sc = new Scanner(new File("songs.txt")); } catch (FileNotFoundException e) { System.out.println("File not found!"); return; }
How can this be rewritten to prevent this error?
The text files:
movies.txt
1, West Side Story, 1961 2, Alladin, 2019 3, Airport, 1970 4, Red Sun, 1971 5, Solaris, 1971 6, Tenet, 2020
songs.txt
1,1, Overture 1,2, Prologue 1,3, Jet Song 1,4, Something's Coming 1,5, Dance at the Gym 2,1, Arabian Nights 2,2, One Jump Ahead 2,4, Speechless 2,5, Friend Like Me
The java files are:
//Song.java public class Song { //private fields private int song_id; private String title; //getter and setter public int getSong_id() { return song_id; } public void setSong_id(int song_id) { this.song_id = song_id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } @Override public String toString() { return song_id+", "+title; } }
//Movie.java import java.util.ArrayList;
public class Movie { //private fields private int movie_id; private String name; private int year_made; private ArrayList songs_list; //getters and setters public int getMovie_id() { return movie_id; } public void setMovie_id(int movie_id) { this.movie_id = movie_id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getYear_made() { return year_made; } public void setYear_made(int year_made) { this.year_made = year_made; } public ArrayList getSongs_list() { return songs_list; } public void setSongs_list(ArrayList songs_list) { this.songs_list = songs_list; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("Movie Id: "+movie_id); sb.append(" Movie Name: "+name); sb.append(" Year made: "+year_made); sb.append(" Songs in this movie: "); for(Song s: songs_list) { sb.append(" "+s); } return sb.toString(); } }
//MovieCollection.java import java.util.ArrayList;
public class MovieCollection { private ArrayList movieList;
//getters and setters public ArrayList getMovieList() { return movieList; }
public void setMovieList(ArrayList movieList) { this.movieList = movieList; } /** * Return a movie information given a movie_id * @param movie_id * @return */ public String getMovieInfo(int movie_id) { Movie movie= null; if(movieList!=null) { for(Movie m: movieList) { if(m.getMovie_id() == movie_id) { movie = m; break; } } } return movie.toString(); }
@Override public String toString() { return "MovieCollection [movieList=" + movieList + "]"; } }
//TestMovies.java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner;
public class TestMovies { public static void main(String args[]) { Scanner sc = null; MovieCollection mc = new MovieCollection(); //Ctreate a file scanner for movies.txt file try { sc = new Scanner(new File("movies.txt")); } catch (FileNotFoundException e) { System.out.println("File not found!"); return; } String line = null; ArrayList movies = new ArrayList(); while(sc.hasNextLine()) {//check if there exist a line or not line = sc.nextLine(); String[] data = line.split(",");//split the linew wrt comma Movie m = new Movie(); //set movie fields after trimming data m.setMovie_id(Integer.parseInt(data[0].trim())); m.setName(data[1].trim()); m.setYear_made(Integer.parseInt(data[2].trim())); m.setSongs_list(new ArrayList()); movies.add(m);//add to movies list } //similarly create scanner for reading songs.txt try { sc = new Scanner(new File("songs.txt")); } catch (FileNotFoundException e) { System.out.println("File not found!"); return; } while(sc.hasNextLine()) {//check if line exists line = sc.nextLine(); String[] data = line.split(",");//split it wrt comma int movieId = Integer.parseInt(data[0].trim()); Song s = new Song();//create song object s.setSong_id(Integer.parseInt(data[1].trim())); s.setTitle(data[2].trim()); for(int i = 0; i < movies.size(); i++) { if(movies.get(i).getMovie_id()== movieId) { movies.get(i).getSongs_list().add(s);//associate this song with needed movie object break; } } } mc.setMovieList(movies);//update movie collection sc.close(); sc = new Scanner(System.in);//now create scanner for user input System.out.println("Enter a movie id: "); int movieId = sc.nextInt();//read movie id sc.close(); System.out.println(mc.getMovieInfo(movieId));//print movie info System.out.println(" Movie Summary: "); System.out.println("Number of movies: "+mc.getMovieList().size()); //get count of moview lists that are empty long count = mc.getMovieList().stream().filter(m->m.getSongs_list().isEmpty()).count(); System.out.println("Movies without any song: "+count); //get movie name with max songs int max = 0; String movieName = null; int totalSongCount = 0; for(Movie m: mc.getMovieList()) { int songCount = m.getSongs_list().size(); totalSongCount+=songCount; if(songCount > max ) { max = songCount; movieName = m.getName(); } } //print System.out.println("Movie with the maximum number of songs: "+movieName); System.out.println("Total Number of Songs: "+totalSongCount);//get total song count as well } }