I need to make a program that prompts the user to input show names, day of new episode and time then store them into an array. But because I'm not allowed to use 1.7, and I have to use version 1.4. I can't use generics, unfortunately.
So I declared the List like this
And I need to make either a bubble sort or selection sort method.
I wrote this method to sort by time, but now I need to sort the shows by alphabetical order.
But with the alphabetical order, I wrote it like this. I KNOW it's wrong, so any idea about how I should write it and output it? Because I'm completely thrown off!
Any help would be great! I need to hand this in today! Thanks in advance.
If you've got code that's working to sort based on time, then you should be able to get it to sort in a different way with a single change. Only the comparison needs to change - to use compareTo on whatever field you want to sort on.
Salma Youssef wrote:I wrote this method to sort by time...
Please DontWriteLongLines. It makes your thread very hard to read,
and it's actually bad coding practice.
I've broken yours up this time, but for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments.
Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Salma Youssef wrote:So I declared the List like this
Well that's where some of your problems start. It should be:
static List<ShowInfo> show = new ArrayList<ShowInfo>(); then you can get rid of about a gazillion casts.
And also, it should be ShowInfo, not showInfo. Class names start with capital letters.
I'm not allowed to use generics because I'm not allowed to use any version of Java besides 1.4.2. It's ancient, I know, and I can't use half the stuff I should be using. But unfortunately, my teacher's insistent on it and won't be wavered. I've managed to make it work by casting (against my own will). Now I've tried changing the sortTime method so that it works alphabetically, but it doesn't seem to be working.
Paul Clapham wrote:Winston, you missed this statement from the original post:
Oops, my apologies. However:
@Salma: You can still save yourself a LOT of unnecessary casting though:I suggest you also look up the List.set() method, because it allows you to swap elements incredibly easily - In fact, you can do it in one line of code without creating a new element.
But your basic problem is that you've written a fabulous amount of code without testing it every step of the way.
One thing I see is that you appear to be mixing up i's and j's in that enormous inner loop of yours, but what you really need to do is to StopCoding (←click) and work out on paper (or with println() statements) what your code is actually doing.
You already know what you want it to do, but it plainly isn't doing it; so you need to work out why. And for that, you need to debug.
And next time, don't write more than about 10 lines of code without compiling it AND testing it.
PS: I already asked you once politely: Please DontWriteLongLines. If you want people to answer your questions, it pays to make it easy for them to read your threads. I broke your enormous lines up again, but I will NOT do it any more.