Cacoon
Cacoon

Reputation: 2538

Getting strings inside a split string

I am loading strings from a text file, eg;

Sunset Blvd 1950.ogg,Sunset Blvd,Paramount Pictures,1950,110,Billy Wilder,4,William Holden,Gloria Swanson,Erich von Stroheim,Nancy Olson

Now I have a class setup that extends from 2 parent classes (Media > Video > Avi/Ogg/etc). And that class holds the following variables;

 public Avi(String title, String fileName, int releaseYear, String studio, String director, String castNames, double runtime, int cast) {
        super(title, fileName, releaseYear, studio, director, castNames, runtime, cast);
    }

Now I load the text file in using a buffer reader and a loop, but heres the problem, the cast names (Which come last in the text file, are also separated with commas but since I am using a splitter already I am not sure how to get every cast member into a simply string such as "Larry Davis,Eddy Murphy,Etc Etc" that can be returned later on. Also using a different splitter for cast names is not an option

Upvotes: 0

Views: 88

Answers (4)

Cacoon
Cacoon

Reputation: 2538

None of these solutions worked but heres what I came up with that worked:

String castNames = "";
                    int splitLength = split.length - 7;
                    for (int i = 0; i < splitLength; i++) {
                        castNames += split[7 + i] + ",";
                    }
                    Avi avi = new Avi(split[1]/*title*/
                            , split[0]/*filename*/
                            , Integer.parseInt(split[3])/*releaseyear*/
                            , split[2]/*studio*/
                            , split[5]/*director*/
                            , castNames/*castnames*/
                            , Double.parseDouble(split[4])/*runtime*/
                            , Integer.parseInt(split[6])/*cast*/);
                return avi;

Upvotes: 0

Rick
Rick

Reputation: 43

I assume that by splitter you mean the String method "split". If so, does your text file always have the same structure ? Meaning is there always the same number of elements before the cast names ? Because the String method "split" can take a second parameter specifying the number of elements to retrieve (cf. link to String API)

Upvotes: 0

Dimitar
Dimitar

Reputation: 134

Having a symbol as the string separator as well as being valid data is not a good idea and results in code that is prone to errors. Of course you can work around that - some people before me have suggested ways to do it - but I strongly recommend that you change your input and remove the ambiguity.

Upvotes: -1

Olayinka
Olayinka

Reputation: 2845

if your cast starts at William Holden, you can do

line.split(",", 8);

Upvotes: 2

Related Questions