So there are a few things I can\'t figure out how to change so it works efficien
ID: 3831177 • Letter: S
Question
So there are a few things I can't figure out how to change so it works efficiently.
Frist, I want to set a time in the time variable not a double and then I want the movies selection program to give you the option to sort by time correctly.
Secondly, I want to the program to read all the words for title until it gets to the time in the file - for example
Pride and Prejudice - should be stored under title
2:00pm should be stored under time
Romance should be stored on genre.
However, when it gets to "and" it stops and says "number format exception: and" .
Plese help me fix these things!
-------------------
import java.util.*;
import java.io.*;
public class MovieSelection
{
public static void main(String[] args)
{
String title;
String genre;
double time;
Movies mv = null;
Scanner sc = null;
ArrayList<Movies> arl = new ArrayList<Movies>();
File inFile = new File("MovieInfo");
try {
sc = new Scanner(inFile);
while (sc.hasNextLine()) {
title = sc.next();
time = Double.parseDouble(sc.next());
genre = sc.next();
mv = new Movies(title, genre, time);
arl.add(mv);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
sc.close();
}
sc = new Scanner(System.in);
int choice;
while (true)
{
System.out.println("1.Sort by Title.");
System.out.println("2.Sort by Genre.");
System.out.println("3.Sort by Time");
System.out.print("Please choose one of the options above>> ");
choice = sc.nextInt();
switch (choice)
{
case 1:
{
Collections.sort(arl, new SortMovieTitle());
System.out
.println(" ___________Sorting based on Location __________");
for (Movies mov : arl)
{
System.out.println(mov.getMovieTitle() + " " + mov.getMovieTime()
+ " " + mov.getMovieGenre());
}
break;
}
case 2:
{
Collections.sort(arl, new SortMovieGenre());
System.out
.println(" ___________Sorting based on Genre__________ ");
for (Movies mov : arl)
{
System.out.println(mov.getMovieTitle() + ' ' + mov.getMovieTime()
+ ' ' + mov.getMovieGenre() );
System.out.println(" ");
}
break;
}
case 3:
{
Collections.sort(arl, new SortByMovieTime());
System.out.println(" ___________Sorting based on Time __________ ");
for (Movies mov : arl)
{
System.out.println(mov.getMovieTitle() + ' ' + mov.getMovieTime()
+ ' ' + mov.getMovieGenre() );
System.out.println(" ");
}
break;
}
default:
{
System.out.println("You have selected an invalid number. Please select option 1 or 2 to sort listings.");
continue;
}
}
break;
}
}
}
-------------------
import java.util.*;
import java.io.*;
public class Movies
{
String movieTitle;
String movieGenre;
double movieTime;
public Movies (String movieTitle, String movieGenre, double mTime)
{
super();
this.movieTitle = movieTitle;
this.movieGenre = movieGenre;
movieTime = mTime;
}
public String getMovieTitle()
{
return movieTitle;
}
public void setMovieTitle(String movieTitle)
{
this.movieTitle = movieTitle;
}
public String getMovieGenre()
{
return movieGenre;
}
public void setMovieGenre(String movieGenre)
{
this.movieGenre = movieGenre;
}
public double getMovieTime()
{
return movieTime;
}
public void setMovieTime(double mTime)
{
movieTime = mTime;
}
@Override
public String toString()
{
return "Movies [movieTitle=" + movieTitle + ", movieGenre=" + movieGenre
+ ", movieTime=" + movieTime + "]";
}
}
-----------------------------
import java.util.Comparator;
public class SortMovieGenre implements Comparator<Movies>
{
@Override
public int compare(Movies mov1, Movies mov2)
{
return mov1.getMovieGenre().compareTo(mov2.getMovieGenre());
}
}
-----------------------
import java.util.Comparator;
public class SortMovieTitle implements Comparator<Movies>
{
@Override
public int compare(Movies mov1, Movies mov2)
{
return mov1.getMovieTitle().compareTo(mov2.getMovieTitle());
}
}
--------------------
import java.util.Comparator;
public class SortByMovieTime implements Comparator<Movies>
{
@Override
public int compare(Movies mov1, Movies mov2)
{
int val= 0;
if(mov1.getMovieTime() < mov2.getMovieTime())
{
val = -1;
}else if(mov1.getMovieTime() > mov2.getMovieTime())
{
val = 1;
}else if(mov2.getMovieTime() == mov2.getMovieTime())
{
val = 0;
}
return val;
}
}
Explanation / Answer
Please make the changes in following classes :
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class MovieSelection
{
public static void main(String[] args)
{
String title;
String genre;
String time;
Movies mv = null;
ArrayList<Movies> arl = new ArrayList<Movies>();
File inFile = new File("/Users/Shipra_Aeron/Documents/workspace/BookMyShow/src/MovieInfo.txt");
try {
FileInputStream in = new FileInputStream(inFile);
BufferedReader reader =
new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
// convert to char and display it
String[] words= line.split(",");
title = words[0];
time = words[1];
genre = words[2];
System.out.print(title+time+genre);
mv = new Movies(title, genre, time);
arl.add(mv);
}
} catch (IOException x) {
System.err.println(x);
}
Scanner sc = new Scanner(System.in);
int choice;
while (true)
{
System.out.println("1.Sort by Title.");
System.out.println("2.Sort by Genre.");
System.out.println("3.Sort by Time");
System.out.print("Please choose one of the options above>> ");
choice = sc.nextInt();
switch (choice)
{
case 1:
{
Collections.sort(arl, new SortMovieTitle());
System.out
.println(" ___________Sorting based on titles __________");
for (Movies mov : arl)
{
System.out.println(mov.getMovieTitle() + " " + mov.getMovieTime()
+ " " + mov.getMovieGenre());
}
break;
}
case 2:
{
Collections.sort(arl, new SortMovieGenre());
System.out
.println(" ___________Sorting based on Genre__________ ");
for (Movies mov : arl)
{
System.out.println(mov.getMovieTitle() + ' ' + mov.getMovieTime()
+ ' ' + mov.getMovieGenre() );
System.out.println(" ");
}
break;
}
case 3:
{
Collections.sort(arl, new SortByMovieTime());
System.out.println(" ___________Sorting based on Time __________ ");
for (Movies mov : arl)
{
System.out.println(mov.getMovieTitle() + ' ' + mov.getMovieTime()
+ ' ' + mov.getMovieGenre() );
System.out.println(" ");
}
break;
}
default:
{
System.out.println("You have selected an invalid number. Please select option 1 or 2 to sort listings.");
continue;
}
}
break;
}
}
}
public class Movies
{
String movieTitle;
String movieGenre;
String movieTime;
public Movies (String movieTitle, String movieGenre, String mTime)
{
super();
this.movieTitle = movieTitle;
this.movieGenre = movieGenre;
movieTime = mTime;
}
public String getMovieTitle()
{
return movieTitle;
}
public void setMovieTitle(String movieTitle)
{
this.movieTitle = movieTitle;
}
public String getMovieGenre()
{
return movieGenre;
}
public void setMovieGenre(String movieGenre)
{
this.movieGenre = movieGenre;
}
public String getMovieTime()
{
return movieTime;
}
public void setMovieTime(String mTime)
{
movieTime = mTime;
}
@Override
public String toString()
{
return "Movies [movieTitle=" + movieTitle + ", movieGenre=" + movieGenre
+ ", movieTime=" + movieTime + "]";
}
}
import java.util.Comparator;
public class SortByMovieTime implements Comparator<Movies>
{
@Override
public int compare(Movies mov1, Movies mov2)
{
int val= 0;
long movieTime1 = Long.parseLong(mov1.getMovieTime().replace(":", ""));
long movieTime2 = Long.parseLong(mov2.getMovieTime().replace(":", ""));
if( movieTime1 < movieTime2)
{
val = -1;
}else if(movieTime1 > movieTime2)
{
val = 1;
}else if(movieTime1 == movieTime2)
{
val = 0;
}
return val;
}
}
In file MovieInfo.txt
Pride and Prejudice,12:30,romance
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.