My java code keeps giving me back StringIndexOutOfBounds for the following code. Need help dealing with this
My java code keeps giving me back StringIndexOutOfBounds for the following code. Need help dealing with this
Movie.java:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Movie
{
public String name;
public int year;
public String genre;
public static ArrayList<Movie> loadDatabase() throws FileNotFoundException {
ArrayList<Movie> result=new ArrayList<>();
File f=new File("db.txt");
Scanner inputFile=new Scanner(f);
while(inputFile.hasNext())
{
String name= inputFile.nextLine();
int year=inputFile.nextInt();
inputFile.nextLine();
String genre= inputFile.nextLine();
Movie m=new Movie(name, year, genre);
//System.out.println(m);
result.add(m);
}
return result;
}
public Movie(String name, int year, String genre)
{
this.name=name;
this.year=year;
this.genre=genre;
}
public boolean equals(int year, String genre)
{
return this.year==year&&this.genre.equals(genre);
}
public String toString()
{
return name+" ("+genre+") "+year;
}
}
MovieDemo.java:
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class MovieDemo
{
public static void printMatchingMovie(ArrayList<Movie>
int numMatches=0;
for (Movie temp : database) {
if (temp.equals(year, genre)) {
System.out.println(temp);
numMatches++;
}
}
System.out.println("Number of matches: "+numMatches);
}
public static void printMatchingMovies(ArrayList<Movie>database, String inputName)
{
int numMatch=0;
for(Movie temp: database){
if(inputName.substring(0,2).equals(temp.name.substring(0,2))){
System.out.println(temp);
numMatch++;
System.out.println("Number of matches: "+numMatch);
}
else {
System.out.println("No matching movies found!");
}
}
}
public static void main(String[] args) throws FileNotFoundException
{
ArrayList<Movie> database=Movie.loadDatabase();
//printMatchingMovie(database, 2004, "Family");
Scanner keyboard=new Scanner(System.in);
System.out.println("Movie search by characters. Enter two characters.");
String inputName= keyboard.nextLine();
System.out.println("Movies that start with "+inputName);
printMatchingMovies(database, inputName);
}
}
te file db.txt is about 4500 lines of code, but i took a few pics for at least an exapmle of what it is.
some examples:
Movie search by characters. Enter two characters.\n
TmENTER
Movies that start with Tm\n
Tmima ithon\n
Tmima ithon\n
Tmima ithon\n
Tmunot Yafo'iyot\n
Tmunot Yafo'iyot\n
Number of matches: 5\n
Test Case 2
MtENTER
Movies that start with Mt\n
Mtv Base: Touching Base\n
Mtvris gemo\n
Number of matches: 2\n
![Moviejava X C MovieDemo.java x
db.txt X
House of Mouse
32
2001
33
Animation
House of Mouse
35
2001
Comedy
House of Mouse
2001
Family
House of Noise
07
866T
Reality-TV
42
43
House of Payne
לל
45
2006
Comedy
House of Payne
97
L7
48
900
Drama
House of Payne
67
9006
Family
51
52
House of Payne
53
9007
Romance
55
House of Pop
0007
Music
57
58
House of Pride
69
Drama
09
House of Rock
Terminal
Build
n1 sec, 684 ms (33 minutes ago)
earch
M M r](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F8cce2df8-418b-4370-a2dd-d59848be66cb%2Fd7ad5900-8103-4f3d-aa3e-2345a3c33c1b%2F1e1m0un_processed.jpeg&w=3840&q=75)
![loviejava x
MovieDemo.java X
E db.txt X
House of Games
1.
2.
200k
Family
3.
House of Games
2004
5.
Game-Show
6.
House of Glam
2010
6.
House of Horrors
Reality-TV
866T
Documentary
1.
House of Jazmin
6007
Comedy
15
16
House of Jazmin
6007
Documentary
House of Jazmin
600
Reality-TV
21.
House of Levy
2010
Reality-TV
House of Lies
2012
Comedy
House of Micky
6007
Comedy
House of Mouse
Build
584 ms (32 minutes ago)
ㄩ 五。](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F8cce2df8-418b-4370-a2dd-d59848be66cb%2Fd7ad5900-8103-4f3d-aa3e-2345a3c33c1b%2Fqodbegc_processed.jpeg&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 3 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)