Forums Register Login

Page 569-571 : Formatting output AND making program in book (Generics and collections)

+Pie Number of slices to send: Send
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 :
movie name/genre/actor/year

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 ?

Here is the output :


Here is the program :



1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.



That part was successfully implemented here :



(the progress for above code in this post : https://coderanch.com/t/533203/java/java/String-split-Runtime-Error-program#2418134 )
+Pie Number of slices to send: Send
the text file to be processed contains this :
________________________________________
wedding crashers/romantic-comedy/2008
the ring/horror/2004
the terminator 3/sci-fi,action/2009
the hangover/comedy/2010
2012/sci-fi/2010
________________________________________

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.



Runtime exception :


+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.

regards
rb

PS : the code and output below...
+Pie Number of slices to send: Send
THE CODE :



THE OUTPUT :

please buy my thing and then I'll have more money:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1593 times.
Similar Threads
Sorting Collections Doubt
Help Please... if u like too.. im new too GUI
K&B Book P570 populateList()
Collections.sort() on Array List
Horror Movies
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
How the new instances print the values?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 05:58:37.