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.
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);
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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.
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?
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.