In java
Okay, I asked this question once and the person who created the
code forgot that this file "db.txt" has hundreds of movies with !
and ' cases
Enter two years. Print movies that were released in those years and that have a name that's less than 6 characters.
Input
1974 1976
output
Movie search by year range. Enter two years.\n Movies with short names that were released between 1970 and 1974\n I dag\n Izumi\n Jamie\n Jamie\n Julie\n Julie\n Kako\n Kako\n Kate\n Kojak\n Kojak\n Kojak\n Kojak\n Kojak\n Leeds\n Maude\n Meshi\n Mily\n Nakia\n Nakia\n Nakia\n Nakia\n Nancy\n Nanou\n Tang\n Them\n Timo\n Toma\n Toma\n Toma\n Toma\n Totte\n Trial\n UFO\n UFO\n Yak\n Yak\n Zoom\n Zut!\n Number of matches: 39\n
If the year entered is out of range, output
Movie search by year range. Enter two years.\n Movies with short names that were released between 2050 and 3000\n No matching movies found!\n
Code :
import java.io.*;
import java. util. Scanner;
public class movieList {
/**
* Opens and reads a file,
*/
public static int findMovieList(File file, int year1,
int year2)
throws IOException
{
BufferedReader reader = new
BufferedReader(new FileReader(file));
String
line;
//Extract lines from the db.txt
int match =
0;
//Match counter..
StringBuilder sb = new
StringBuilder();
System.out.print("\n Movies with short
names that were released between " + year1 + " and "+ year2 + ":
\n");
while ((line = reader.readLine()) !=
null)
{
sb.append(line + "\n");
String[] values =
line.split(";");
//Splitting Data from the file..
int year =
Integer.parseInt(values[0].trim()); //Storing the
year
if (year ==(year1) || year
==(year2)) //Checking the
year1 and year2
if(values[2].length() < 6)
{
//Checking length of the movie < 6
System.out.println((values[2])); //Print
only the year1 & yer2 movie's names
match = match +
1;
//Counts total number of matches:
}
}
reader.close();
//Close the reader..
return match;
}
public static void main(String[] args)throws
Exception
{
// We need to provide file path as the
parameter:
// double backquote is to avoid compiler
interpret words
// like \test as \t (ie. as a escape
sequence)
File file = new
File("/home/Desktop/java/db.txt");
//Please provide your path to the file...
System. out. print( "Movie search by year
range.\n\nEnter two years : ");
int year1;
int year2;
Scanner sc = new
Scanner(System.in).useDelimiter("\\s");
year1 = sc.nextInt(); // It will not
leave until the user enters data.
year2 = sc.nextInt(); // We can read
specific data.
int match = findMovieList(file,
year1,year2); //Function call to find movies..
if (match == 0 )
System.out.print("\n
No matching movies found! ");
else
System.out.println("\n
Number of matches: " + match);
sc.close();
}
}
input db.txt: (Sample for this program)
Output:
___________________________________________________________________________________________
Please upvote if you are satisfied with this solution...
In java Okay, I asked this question once and the person who created the code forgot...