4

I am doing a project for my Java class and I can't figure out how to find the index of the max time and I need help to get this method working.

public class ReservationList {
        Reservations[] reservationsArray;
        String name, phone, time;
        int index, numberInAParty;

    public ReservationList(int size) {
        reservationsArray = new Reservations[size];
    }

    //Adds items to the reservations array
    public void addArrayItem(int index, Reservations reservation) {        
        this.reservationsArray[index] = reservation;
    }

    //Finds the index of the highest number
    public static int indexOfMaxInRange (ReservationList list, int low, int high) {
        int timeZero = Integer.valueOf(list.reservationsArray[0].time.replace(":", ""));        
        int max = timeZero;        
        int maxIndex = 0;    
        for (int i = low; i < high; i++) {            
            int time = Integer.valueOf(list.reservationsArray[i].time.replace(":", ""));         
            if (time > max) {
                System.out.println("Pass");
                maxIndex = i;
                //max = Integer.valueOf(list.reservationsArray[i].time);
            }
        }
        return maxIndex;
    }

    //Swaps array elements
    private static void swapElement (ReservationList list, int indexOne, int indexTwo) {
        Reservations temp = list.reservationsArray[indexOne];
        list.reservationsArray[indexOne]= list.reservationsArray[indexTwo];
        list.reservationsArray[indexTwo]= temp;
    }

    //Sorts through the array
    protected static void sortArray(ReservationList list) {  
        for(int i = list.reservationsArray.length;i >= 0; i--){
            int big = indexOfMaxInRange(list,0,i);
            swapElement(list, big,i);
        }
        for(int i=list.reservationsArray.length;i>0;i=i-1){
            swapElement(list,i,i-1);
        }   
    }
}

public class Reservations {

    protected String name;
    protected String phone;
    protected int numberInAParty;
    protected String time;


    public Reservations() {
        this.name = "";
        this.phone = "";
        this.time = "";
        this.numberInAParty = 0;
    }

    public Reservations(String name, String phone, String time, int numberInAParty, int size) {
        this.name = name;
        this.phone = phone;
        this.time = time;
        this.numberInAParty = numberInAParty;
    }

    public void printReservation (Reservations x) {
        System.out.println("Name: " +  x.name);
        System.out.println("Phone number: " + x.phone);
        System.out.println("Time: " + x.time);
        System.out.println("Party number: " + x.numberInAParty);
    }

}

Here is how the list is initialized

private void loadArray (String fileName, ReservationList list) throws IOException, FileNotFoundException {
    BufferedReader reader = new BufferedReader (new InputStreamReader(new FileInputStream(fileName)));     
    Reservations temp = new Reservations();
    String reservation;

    int i = 0;
    String delimiters = "[ ]+";        

    try {                        
        while ((reservation = reader.readLine()) !=null) {
            String[] splitReservation = reservation.split(delimiters);
            temp.name = splitReservation[0];
            temp.phone = splitReservation[1];
            temp.numberInAParty = Integer.valueOf((String)splitReservation[2]);
            temp.time = splitReservation[3];

            list.addArrayItem(i, temp);
            list.sortArray(list);
            ReservationsTextArea.append(list.reservationsArray[i].name + "  " + list.reservationsArray[i].phone + "  " + list.reservationsArray[i].numberInAParty + "  " + list.reservationsArray[i].time + "\n");                                                
            i++;
        }


    } catch (NumberFormatException e) {
        System.out.println(e.getMessage());
    }                        
}

Let me know if you need more of the code. This is my first time posting so let me know if I did anything wrong.

I am inputting an object that contains a name, phone number, party amount, and time. This method is supposed to sort the times so that the smallest time is first and latest time is last.

I get a nullpointerexception after it prints out pass, it is just there for debugging purposes. It works if I replace max with 0 however and I don't understand why. Let me know if you need anything more.

I didn't create some of these methods so don't criticize any of it please. All I want it to fix what is specified. Sorry that it is ridiculously long. Our group used the swing editor in NetBeans to make it to save time, please don't comment about how Im not gonna learn anything etc. I know.

The method that adds the objects are loadArray and DisplayAllButtonActionPerformed calls that method

I'm also aware that sortArray is in a loop and until it works I am just keeping it there.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

2 Answers2

2

Change your loadArray code so that a new Temp Object is created in each iteration of the loop

while ((reservation = reader.readLine()) !=null) {
   Reservations temp = new Reservations();
   temp.name = splitReservation[0];
   // etc

otherwise you are always working on the same Object

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

I haven't been able to narrow down the NullPointerException, so you may want to debug the file that you are reading in and check the array after reading in all your data.

The problems I did find so far, though, are that the sortArray is wrong. You initialize i = list.length, which would throw an IndexOutOfBoundException. You also have the same for-loop twice for some reason.

So here is the fix for that (which I verified sorts ascending).

protected static void sortArray(ReservationList list) {
    for (int i = list.reservationsArray.length - 1; i >= 0; i--) {
        int big = indexOfMaxInRange(list, 0, i);
        swapElement(list, big, i);
    }
}

As for the max index method. There has to be somewhere you are getting a null object in order for you to get that error. From my simple testing, this works for me.

public static int indexOfMaxInRange(ReservationList list, int low, int high) {
    int max = Integer.MIN_VALUE; // Set this to be the lowest possible integer
    int maxIndex = low;

    for (int i = low; i < high; i++) {
        String timestamp = list.reservationsArray[i].time.replace(":", "");
        int time = Integer.valueOf(timestamp);

        if (time > max) {
            maxIndex = i;
            max = time;
        }
    }
    return maxIndex;  // note if low >= high, low is returned
}

You also don't need to sort the array every single time you add a Reservations object, so do something like

 try {                        
    while ((reservation = reader.readLine()) != null) {            
        String[] splitReservation = reservation.split("\\s+"); // this is the proper delimiter you want

        Reservations temp = new Reservations();
        temp.name = splitReservation[0];
        temp.phone = splitReservation[1];
        temp.numberInAParty = Integer.valueOf(splitReservation[2]);
        temp.time = splitReservation[3];

        list.addArrayItem(i, temp);                                      
        i++;
    }
} catch (NumberFormatException e) {
    System.out.println(e.getMessage());
}     
ReservationList.sortArray(list); // this is a static method, so treat it like one  
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • @Sandpiper131 - I'm sorry this code didn't help you, but that doesn't give you the right to completely invalidate the work I put into helping you. I recreated all the necessary pieces of your project and got all these methods to work fine. If you truly want to learn how to code, learn to use a debugger to walk through your code. Even forget the GUI and build out smaller modules like I did to make sure your logic works. – OneCricketeer Dec 16 '15 at 04:33