0

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

4
  • 4
    can you share your code, please. From what I read the I assume Billy Wilder, William Holden,Gloria Swanson etc are the names of cast members and if you split the string using "," as your delimiter you would get these string individually into different cells of an array, So did not understand your query properly Commented Apr 30, 2015 at 7:42
  • Maybe use standard CSV, and standard parser for it instead of writing 'bicycle' code ? Commented Apr 30, 2015 at 7:43
  • You want to split a string but the splitter isn't working and you can't use a different splitter? Commented Apr 30, 2015 at 7:49
  • Its for a test, I understand that its a bit odd(stupid) but thats how I will receive the input, and thats how I need to dealt with it, I found my own solution though so not to worry! :) Commented Apr 30, 2015 at 10:07

4 Answers 4

2

if your cast starts at William Holden, you can do

line.split(",", 8);
Sign up to request clarification or add additional context in comments.

1 Comment

this gets the values before the names, not the names themselves
0

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)

Comments

0

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;

Comments

-1

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.

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.