I am brand new to java and I am creating a file directory that stores movies and information about the movies. The movies and info are stored in a file like this:
Movie Title
Rating Year Reviews Month Day Year
Title on first line and other info on the second line. So with multiple movies it looks like this:
Movie Title
Rating Year Reviews Month Day Year
Movie Title
Rating Year Reviews Month Day Year
Movie Title
Rating Year Reviews Month Day Year
...and so on. I have a method that prints the movies to the console but I am running into a problem formatting the movies correctly. The movie info should be formatted like this:
Star Wars PG 1977 5 stars 1/5/2018
Each column should be aligned properly. The title, rating, and year should be left aligned; the number of stars and date right aligned.
The First time it prints the movie info out exactly how I want, but when I add multiple movies it messes things up. Here is my code, I believe the problem is in the listMovies method where I try to format the output, but I included all my code in case something is messed up somewhere else.
public class Directory
{
private static final String dir = "data/cs2410-directory.data";
String mTitle,mRat,mRel,mRev,mM,mD,mY;
public Directory()
{
mTitle = "Movie Title";
mRat = "Rating";
mRel = "Year";
mRev = "Reviews";
mM = "Month";
mD = "Day";
mY ="Year";
insertMovie();
listMovies();
}
private void insertMovie()
{
PrintWriter dirIn = null;
try
{
dirIn = new PrintWriter(new FileOutputStream(dir,true));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
dirIn.println(mTitle);
dirIn.println(mRat+" "+mRel+" "+mRev+" "+mM+" "+mD+" "+mY);
dirIn.close();
System.out.print("The following movie has been added to the directory:\n");
System.out.print(mTitle+" ("+mRel+") "+mRat+"\n");
System.out.print("Stars: "+mRev+"\n");
System.out.print("Last Watched: "+mM+"/"+mD+"/"+mY+"\n");
}
private void listMovies()
{
Scanner dirOut = null;
try
{
dirOut = new Scanner(new FileReader(dir));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
while (dirOut.hasNext()) //PROBLEM
{
System.out.printf("%-15s%-10s%-10s%5s%10s/%s/%s\n", dirOut.nextLine(),dirOut.next()
,dirOut.next(),dirOut.next(),dirOut.next(),dirOut.next(),dirOut.next());
}
dirOut.close();
}
public static void main(String[] args)
{
new Directory();
}
This is the output after one movie (running program once) which is how it is supposed to look:
The following movie has been added to the directory:
Movie Title (Year) Rating
Stars: Reviews
Last Watched: Month/Day/Year
Movie Title Rating Year Reviews Month/Day/Year
This is after running it twice:
The following movie has been added to the directory:
Movie Title (Year) Rating
Stars: Reviews
Last Watched: Month/Day/Year
Movie Title Rating Year Reviews Month/Day/Year
Movie Exception in thread "main" java.util.NoSuchElementException
Title Rating Year/Reviews/Month
at java.base/java.util.Scanner.throwFor(Scanner.java:858)
at java.base/java.util.Scanner.next(Scanner.java:1381)
at cs2410.assn3.directory.Directory.listMovies(Directory.java:71)
at cs2410.assn3.directory.Directory.<init>(Directory.java:32)
at cs2410.assn3.directory.Directory.main(Directory.java:79)
This is after running it 5 times:
The following movie has been added to the directory:
Movie Title (Year) Rating
Stars: Reviews
Last Watched: Month/Day/Year
Movie Title Rating Year Reviews Month/Day/Year
Movie Title Rating Year/Reviews/Month
Day Year Movie Title Rating Year/Reviews/Month
Day Year Movie Title Rating Year/Reviews/Month
Day Year Movie Title Rating Year/Reviews/Month
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:858)
at java.base/java.util.Scanner.next(Scanner.java:1381)
at cs2410.assn3.directory.Directory.listMovies(Directory.java:71)
at cs2410.assn3.directory.Directory.<init>(Directory.java:32)
at cs2410.assn3.directory.Directory.main(Directory.java:79)
dirIn.println(mTitle); dirIn.println(mRat+" "+mRel+" "+mRev+" "+mM+" "+mD+" "+mY); dirIn.close();