In my program, I'm reading a file containing information about movies and extracting movie IDs and actor names. Then I'm storing the movie ID and actor name for each movie in an object and adding the objects into an array list. Then I'm prompting the user to enter an actor name and I want to check the entire list to see whether or not the actor is in the list.
I do this with a while loop: while(!cast.contains(actor1)), however, I'm comparing the user's input to the objects within the list and not the actor name associated with the objects.
import java.util.Scanner;
import java.io.File;
import java.util.ArrayList;
public class Assignment2 {
public static void main(String[] args) throws Exception {
File movieFile = null;
if(args.length > 0)
movieFile = new File(args[0]);
Scanner sc = new Scanner(movieFile);
String movieLine, actorName;
int movieID, index1, index2, count = 0;
ArrayList<MovieCast> cast = new ArrayList<>();
MovieCast listObj;
sc.nextLine(); // skip first line (movie_id, title, cast, crew)
while(sc.hasNext()) {
movieLine = sc.nextLine();
index1 = movieLine.indexOf(',');
movieID = Integer.parseInt((movieLine.substring(0, index1)).trim());
index1 = movieLine.indexOf("cast_id"); // in case movie contains name
if(index1 > -1) {
index1 = movieLine.indexOf("name", index1);
index1 += 10; // moved to beginning of actor's name
index2 = movieLine.indexOf(',', index1);
actorName = movieLine.substring(index1, index2 - 2);
listObj = new MovieCast(movieID, actorName);
cast.add(listObj);
count++;
}
}
for(int i = 0; i < 10; i++) // for testing
System.out.println(cast.get(i).movieID + "\t" + cast.get(i).actorName);
sc.close();
Scanner scan = new Scanner(System.in);
String actor1 = " ";
while(!actor1.isEmpty()) {
System.out.print("Enter actor 1's name or enter nothing to stop: ");
actor1 = scan.nextLine();
if(actor1.length() == 0)
return;
while(!cast.contains(actor1)) {
System.out.println("No such actor.");
System.out.println("Enter actor 1's name or enter nothing to stop: ");
actor1 = scan.nextLine();
if(actor1.length() == 0)
return;
}
}
}
}
I was wondering how I could access the actor name within the object for comparison.