I will read details of a movie from a text file, dump them into an array list, sort the array list . Print list before and after it was sorted.
The text file has lines in this format :
How do i save each of the details of a movie in separate strings (say, of a movie object)...ie store the words after every / in a new variable.
I made a basic program which is ok, but i want my output not to have any slash / . How can i achieve this ?
You can create a Movie class- with the fields as there in the text file- Name, genre, actor, year. Then when you read a line from the file- you can use the split() function of the String to split the sentence based on the separator. And each string thus obtained can be used to create a new Movie instance and then add the instance to the ArrayList. You need to implement Comparable interface in the Movie class so that Collection.sort() can sort the contents of the list correctly. Also you can override toString method in Movie class so that it displays a neatly formatted String when ever the name of the Movie instance is used.
Mohamed Sanaulla wrote:Then when you read a line from the file- you can use the split() function of the String to split the sentence based on the separator. And each string thus obtained can be used to create a new Movie instance and then add the instance to the ArrayList.
the text file to be processed contains this : ________________________________________
the terminator 3/sci-fi,action/2009
Now the full code which uses the above code to process this text file : This code throws runtime exception which is shown after the program.
Mohamed Sanaulla wrote:Look at the separator used for the split function. (The thread you had posted which had the run time error use- "\" as the separator and this one is using "/")- Did you try using just "/"?
Suggestion: >> Before operating on the Array using indexes- Ensure that the array is of the required size
New program works !!!
Did all that you told me, thanks for giving those crucial clues. One small problem remains in the output, tab between title and year is there only for first movie and not for the others ???
now only one feature to be added, ie sorting as per given parameter like title, year etc. will add it soon.