0

I have a Movie object where the data members are a String title, int year, and ArrayList actors. I am a bit confused on how I can add this ArrayList<String> to my tree. I am reading this information from a file for example:

Forrest Gump/1994/Tom Hanks
Star Wars/1977/Mark Hamill,Carrie Fisher,Harrison Ford

So far, I have been able to add everything else except for the ArrayList. I am thinking that I will need to also line.split the contents of the array. Also, some of the movies do not have multiple actors as shown in the example so I am not sure how to go about implementing this. I have tried a few different ways, but have ended up with an IndexOutOfBoundsException.

Here is what I have so far:

try{
        Scanner read = new Scanner( new File("movies.txt") );
        do{
            ArrayList<String> actorList = new ArrayList<String>();
            String line = read.nextLine();
            String [] tokens = line.split("/");
            //I think I need to add another split for commas here.
            //actorList.add() here

            tree.add( new Movie(tokens[0], Integer.parseInt(tokens[1]), actorList ));
        }while( read.hasNext() );
        read.close();
    }catch( FileNotFoundException fnf ){
        System.out.println("File not found.");
    }

Her is my Constructor if needed:

public Movie( String t, int y, ArrayList<String> a ){
    title = t;
    year = y;
    actors = a;
}
2
  • Have you debugged your code for possible candidates of IndexOutOfBoundsException? Post your complete stacktrace. And yes, you need to convert your tokens[2] to List. Commented May 6, 2015 at 5:53
  • If you ask about an exception, post the code causing the exception, and the stack trace of the exception. Tell us which line is referenced by the stack trace. Commented May 6, 2015 at 5:55

3 Answers 3

3

Hopefully, below code should work. Split your comma separated actor list, convert that String array to a List and add this List to your ArrayList. Use Arrays.asList() as a neat implementation.

try{
        Scanner read = new Scanner( new File("movies.txt") );
        do{
            ArrayList<String> actorList = new ArrayList<String>();
            String line = read.nextLine();
            String [] tokens = line.split("/");
            actorList.addAll(Arrays.asList(tokens[2].split(",")));

            tree.add( new Movie(tokens[0], Integer.parseInt(tokens[1]), actorList ));
        }while( read.hasNext() );
        read.close();
    }catch( FileNotFoundException fnf ){
        System.out.println("File not found.");
    }
Sign up to request clarification or add additional context in comments.

7 Comments

I'm not the downvoter but it won't compile...tokens[2] is a String not an array - Now, you can modify this code to do what Tony shows in his answer below - but before you do that - please read the comment I gave him.
@alfasin thanks for pointing out the mistake. Though I would urge the downvoter to at-least show some dignity and post a comment why downvote the answer. It's bad practice to downvote and not put any comment why.
@alfasin, in my code, I have added all the elements from the immutable list to the one that is already initialized. So, even after using my code, OP can use all the methods of a List.
Yes, this is much better, I've upvoted to compensate ;)
@Aakash Hey this works! But, I have an error, whenever the movie does not have any actors I get an IndexOutOfBoundsException. For example: Raging Bull/1980/ this would give me exception. I need it to work for this condition.
|
1

You can split the last token by comma and insert each one of the strings that are created into the actorList:

...
String [] tokens = line.split("/");
String lastToken = tokens[tokens.length-1];
if (tokens.length == 3) {
    String[] actors = lastToken.split(",");
    for (String actor : actors) {
        actorList.add(actor);
    }
}
...

5 Comments

This works as well, except how can I make it work for lets say a movie does not have any actors listed for example: Raging Bull/1988/ would output Raging Bull/1988/[1988] it prints out the last token again if there is nothing there and I need it to function for this condition.
@JCCS by adding a check to see if the actors are there, see additional if condition I've added to the answer.
Its not adding those without a title to the list with this condition. Any ideas?
@JCCS sounds like you'll have to do more than a "split": you'll have to build a parser that checks which parts are there...
Got it working! Using the code from the other user and your check solved the issue thanks both of you.
0

Try this:

String actors = tokens[tokens.length-1];
actorList = Arrays.asList(actors.split(","));

11 Comments

Though it works, it returns an immutable actorList which is of type List (instead of ArrayList). I'm not sure that's what the OP wants...
@alfasin the returned list is not immutable. It's fixed-size.
@JBNizet that's what I call immutable :) of course you can modify the objects it contains - but you can't modify the List itself (i.e. use .add() ).
You can modify the list itself. You just can't change its size. list.set() will work fine. Collections.sort(list) will work fine. It's thus not immutable, but fixed-size.
@JBNizet I would still argue that the it's not the list you're modifying but only the objects in it (kind of like "swap" which changes pointers but not the actual object or their containers which are the links of the list in our case). That said, I didn't know that "sort" would work - so thanks for teaching me something new!
|

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.