3

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. The program prompts the user for the name of the show, day a new episode premieres, and time, which are stored in the array. It's sorts by the keys name, day, and time (alphabetically and numerically).

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

I made a class and used an array to save the inputted shows. Here is the class:

public class showInfo   
{  
String name;   
String day;   
int time;       
}

I've inputted the shows like this:

public static void addShow() throws IOException
    {
    //initialize counter
    int i = 0;
    arr = new showInfo[i];

    showInfo temp = new showInfo();

    //input information
    do
    {
        System.out.print("Enter the name of show: ");
        String showName = br.readLine();
        temp.name = showName;

        System.out.print("Enter which day of the week a new episode premieres: ");
        String showDay = br.readLine();
        temp.day = showDay;

        System.out.print("Enter time in 24-hour format (e.g. 2100, 1900): ");
        int showTime = Integer.valueOf(br.readLine()).intValue();
        temp.time = showTime;

        i++;

        System.out.print("Would you like to add another show? (y/n) ");
    }
    while((br.readLine().compareTo("n"))!=0);
}

In order to sort by time, I've written the following method:

public static void timeSort()
{
    int min;
    for (int i = 0; i < arr.length; i++) 
    {

        // Assume first element is min
        min = i;
        for (int j = i+1; j < arr.length; j++) 
        {
            if (arr[j].time < arr[min].time) 
            {
                min = j;
            }
        }

        if (min != i) 
        {
            int temp = arr[i].time;
            arr[i].time = arr[min].time;
            arr[min].time = temp;
        }
    }
    System.out.println("TV Shows by Time");
    for(int i = 0; i < arr.length; i++)
    {
        System.out.println(arr[i].name + " - " + arr[i].day + " - " + arr[i].time + " hours");
    }
}

My problem is that when I call it and output it in the main, it only shows "TV Shows by Time" text, but not the shows. Why is this? (Should I show my full code?)

I think my issue has something to do with the counter, but I'm not sure how I'd fix that since the user is supposed to input as many shows as they'd like and the amount of shows is modified and deleted in other methods.

Any help would be great! Thanks in advance!

6
  • You need to show us where and how are you populating your array. Commented Sep 14, 2013 at 18:16
  • Have you verified that your array has length > 0...or are you sure that the array actually has values in it when the method is called? Commented Sep 14, 2013 at 18:20
  • It's always helpful to show the exact output. If you don't see "- - - - hours"... Commented Sep 14, 2013 at 18:21
  • At the start of the timeSort method add System.out.println("number of shows = " + arr.length); to verify that you actually have some shows. Commented Sep 14, 2013 at 18:22
  • Looks like you're just swapping times rather that swapping entries in the array. Should be: showInfo temp = arr[i]; arr[i]=arr[min]; arr[min]=temp; Commented Sep 14, 2013 at 18:25

2 Answers 2

2

If you're not prohibited from using Java's Collections API, use Arrays.sort(T[],Comparator). Then your program can maintain three static Comparator objects -- one for show, one for day, and one for time. E.g.,

  static Comparator<showTime> daySort = new Comparator<showTime>(){
    public int compare(showTime st1, showTime st2){
      return st1.day.compareTo(st2.day);
    }
    public boolean equals(Object o){
      return this==o; /* Easy way out for this method. */
    }
  };

Then you have a single sort method (if it's even necessary) that calls Arrays.sort:

  public static void sort(){
    Arrays.sort(arr, daySort);
  }

See Arrays and Comparator for more information.

Sign up to request clarification or add additional context in comments.

2 Comments

The thing is, I'm limited on the way I can sort. My teacher wants me to use either bubble sorting or selection sorting. So I can't really use this. :(
That makes sense. Just know that's how you should do it IRL.
2

I think the sorting is not working because there are no elements present in the ShowInfo[] array to sort.

You are initializing the ShowInfo array inside the addShow method:

//initialize counter
int i = 0;
arr = new showInfo[i];

And you are creating a new ShowInfo:

ShowInfo temp = new showInfo(); 

but this temp was never assigned to any array element. So, every time you are calling this method to add a new show, you are actually creating a new ShowInfo[] array of length 0 and an instance of ShowInfo which was never assigned to any array element.

You need to initialize the array/counter only once, so separate the array/counter initialization code from their use (you should not keep these initialization statements inside any of your delete/modify/sort methods):

private static int i=0;
private static ShowInfo[] arr=new ShowInfo[NO_OF_SHOWINFOS];

With these, your addShow method will change into something like this:

public static void addShow() throws IOException
    {
    //initialize counter
    //int i = 0;
    //arr = new showInfo[i];

    //showInfo temp = new showInfo();

    //input information
    do
    {
        showInfo temp = new showInfo();

        System.out.print("Enter the name of show: ");
        String showName = br.readLine();
        temp.name = showName;

        System.out.print("Enter which day of the week a new episode premieres: ");
        String showDay = br.readLine();
        temp.day = showDay;

        System.out.print("Enter time in 24-hour format (e.g. 2100, 1900): ");
        int showTime = Integer.valueOf(br.readLine()).intValue();
        temp.time = showTime;

        //i++;
        arr[i++]=temp;
        System.out.print("Would you like to add another show? (y/n) ");
    }
    while((br.readLine().compareTo("n"))!=0);
}

}

1 Comment

While reading the input you can build a List<ShowInfo> and then use list.toArray to convert it to an array. Then you don't have to know the number of shows before you start inputting them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.