• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in sorting the collections.

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Fame Devon
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Fame Devon
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did you close the writer which wrote that text file?
 
Fame Devon
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think I did close the writer here...
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks @CampBell Ritchie...

I got the problem figured thanks to you.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic