0

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.

0

2 Answers 2

1

Two methods to access list by object property.
1. Iteration

MovieCast searchMovie(String cast) {
    for(MovieCast movie: movies) {
        if(movie.getCast().equals(cast)) {
            return movie;
        }
    }
    return null;
}

2. Use Map

//Key as cast(actor)
//Value as Movie Object
HashMap<String, MovieCast> movies= new HashMap<>();

Java 8 If you are coding in Java 8 you can use streams to efficiently achieve this.

public boolean containsCast(final List<MovieCast> list, final String cast){
    return list.stream().filter(o -> o.getCast().equals(cast)).findFirst().isPresent();
}
Sign up to request clarification or add additional context in comments.

Comments

0

use java8 streams to filter / collection based on your search criteria

public boolean containsCast(final List list, final String cast){ return list.stream().filter(o -> o.getCast().equals(cast)).findFirst().isPresent(); }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.