简体   繁体   中英

Sorting with Strings and int in array

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!

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. Eg,

  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.

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);
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM