permaculture playing cards*
The moose likes Java in General and the fly likes Problem in sorting the collections. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem in sorting the collections." Watch "Problem in sorting the collections." New topic
Author

Problem in sorting the collections.

Fame Devon
Greenhorn

Joined: Oct 05, 2012
Posts: 13
Hello frnds,
This is my first post in the forum and the second program which I've made on my own.
The program's objective is: Have the user enter movie names.
Store the names in a text file
Retrieve the movie names from the text file and put them in a list.
Sort the names in the list.(I'm having problems in doing this)
Display the result.

This was my initial objective. I've tried to improvise a bit, but the code is almost the same.

Now, everything is working fine, but the list is not getting sorted(I've used Collections.sort() but its not working for some reason. Please help me out with that. Also, as I'm new in coding, I'd love to hear your opinion and suggestions about the code.


Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Hi,

Welcome to CodeRanch!

Please take a close look at below lines from your code:What exactly are you trying to do?

Hint: Where is the list which contains all the lines from file?

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Fame Devon
Greenhorn

Joined: Oct 05, 2012
Posts: 13
Thanks Anayonkar Shivalkar, I got the mistake. But now, how do I break this line and enter the pieces in a list.
Or should I enter the inputs in different lines in the first place.

I tried doing the latter, but then all the movie names end up in different lists. I also tried using String[] strBrk = strLine.split(" ");, but then I'm not able to add it to the list.

So, basically my question is... I have String[], and I need String. What should I do?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8063
    
  22

Fame Devon wrote:This was my initial objective. I've tried to improvise a bit, but the code is almost the same.

First, let me say that writing down objectives is a very good habit to get into. Keep it up.

Second: Part of the art of programming is to break up problems in to manageable chunks. Right now, your code is all in one big clump. One way to go about it would be to come up with methods for each of your major choices, eg:Do you see how much more readable it is? And all you need to do is move all that "body" code to its own method.
You'll discover, as you get more into programming, that you want to keep your main() methods as small as possible.

You might also want to have a look at Java's switch statement, which makes this sort of "choice" logic even more readable (in my opinion), but don't worry about it too much; your if...else is also fine.

Also, I'd think about creating a Movie class rather than just handling Strings. Right now it may only have a name, but later on you'll be able to add other pieces of info to it (eg, producer, running time) and store it to/read it from your file as well.
And then your option 2 might look like:
List<Movie> list = getMoviesFrom(movieFile);

HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Fame Devon
Greenhorn

Joined: Oct 05, 2012
Posts: 13
Thanks a lot for the feedback Winston Gutkowski .
I made 5 methods and kept them in a different package with only instances in the main. And as you said, the next objective I have is to make a movie object with movie name, genre, lead actor and year of release. I'll be following your advice for that.

And I got my code working. Thanks everyone for the help.


Also, the problem with the sorting of collections was solved, putting up the code used.

I kind of devised this way on my own after reading the suggestions here and reading the API for an hour, so don't know if this is the right way or not. Please suggest if anyone notices any scope of improvement.

Thank You
Fame Devon
Greenhorn

Joined: Oct 05, 2012
Posts: 13
I have one more problem. MY friend who tested the code for me asked me about a problem.

When this project is running, the out.txt file can't be deleted. But even when the txt file is cleared and I've selected the endMovie which executes System.exit(0);, when I try to delete the out.txt file, I get a message saying

"The action can't be completed because the file is open in Java(TM) platform SE binary."

Honestly, I don't have a clue. . Can anyone help me with that?

Thank You.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Where did you close the writer which wrote that text file?
Fame Devon
Greenhorn

Joined: Oct 05, 2012
Posts: 13

I think I did close the writer here...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
How do you know the Writer will be closed? How do you know there won’t be an Exception in that block before you reach the close statement? How do you know that block will be selected at all? Are you opening such writers elsewhere?
Please search for closing writers in a finally block; I am too busy to search myself just at the moment. A finally will ensure the Writer is definitely closed.
Fame Devon
Greenhorn

Joined: Oct 05, 2012
Posts: 13
Thanks @CampBell Ritchie...

I got the problem figured thanks to you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
You’re welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in sorting the collections.