• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting with Strings and int  RSS feed

 
Salma Youssef
Greenhorn
Posts: 20
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My assignment asks me to make a TV show program, where I can input shows, delete, modify and sort them. What I'm stuck on is the sorting part. With the show, it asks for the name, day a new episode premieres, and time. Those are the keys I need to sort it by.

The program prompts the user to input one of those keys, then the program needs to sort (sorting by day will sort alphabetically).

I made a class and used an array. Here is the class:



And the method to sort by time in the code:



When I call it and output it in the main, it only shows "TV Shows by Time" and not the list. Why is this?

Also, I need to make ONE method that I will be able to use to sort both the day AND the name (both Strings). How can I do this without using those specific arrays (arr[i].name, arr[i].day) in the method?

I need some way to do this WITHOUT using an ArrayList! I've already done so much and I don't want to go back and change everything and risk messing something up. Any help would be greatly appreciated! Thanks in advance!
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Salma,
Welcome to java ranch.
I would recommend if you could post the full code including the main method reason being its hard to deduce what does the variable arr[] stores from the code that you have posted.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Salma Youssef wrote:Those are the keys I need to sort it by.

My suggestion: Look at the Comparator interface (java.util.Comparator ←click). That allows you to order objects any way you like, and once you have set one up, you can use Arrays.sort() with it (or alternatively, write your own that uses it).

Winston
 
Salma Youssef
Greenhorn
Posts: 20
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Dutt wrote:Hello Salma,
Welcome to java ranch.
I would recommend if you could post the full code including the main method reason being its hard to deduce what does the variable arr[] stores from the code that you have posted.


Most of it works, except the sorting! If there's anything else wrong with it, I would appreciate it if you could point it out and example! Thank you!

I already posted the class, so here's is the majority of my code thus far:

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your problem may be related to this line:

In other words, you always create the array as being empty.

If you know how big the array is going to be, make sure it's initialised properly to that size as one of the first things you do. If you don't know how big the array is going to be...then I'm afraid you shouldn't be using an array because you can't resize them.
 
Salma Youssef
Greenhorn
Posts: 20
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:I think your problem may be related to this line:

In other words, you always create the array as being empty.

If you know how big the array is going to be, make sure it's initialised properly to that size as one of the first things you do. If you don't know how big the array is going to be...then I'm afraid you shouldn't be using an array because you can't resize them.


Yes, but then I've written "i++" in the do-while loop. Wouldn't that be increasing the index? Also, the issue isn't in the addShow method, but when I tested it, it worked fine!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Salma Youssef wrote:Yes, but then I've written "i++" in the do-while loop. Wouldn't that be increasing the index?

It will increase i. It won't have any effect on the array at all. At the point the array is created i is zero, so you create a zero length array, which means it will always be zero length.

Since that method is the only place where you assign to arr, then arr will only ever reference a zero length array. And that explains why timeSort() behaves the way you see.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!