aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Page 569-571 : Formatting output AND making program in book (Generics and collections) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Page 569-571 : Formatting output AND making program in book (Generics and collections)" Watch "Page 569-571 : Formatting output AND making program in book (Generics and collections)" New topic
Author

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

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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 :




SCJP 6. Learning more now.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

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 | My Blog
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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 : http://www.coderanch.com/t/533203/java/java/String-split-Runtime-Error-program#2418134 )
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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 :


Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

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
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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...
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

THE CODE :



THE OUTPUT :

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Page 569-571 : Formatting output AND making program in book (Generics and collections)
 
Similar Threads
K&B Book P570 populateList()
Sorting Collections Doubt
Horror Movies
Help Please... if u like too.. im new too GUI
Collections.sort() on Array List