Java indexOfMaxInRange

I am making a project for my Java class and I cannot figure out how to find the maximum time index, and I need help to make this method work.

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

}

This 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 code. This is my first post, so let me know if I did something wrong.

I entered an object that contains a name, phone number, number of parties and time. This method should sort the time so that the least time is the first and last last.

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

, . , , , . , . swing NetBeans, , , , .. .

, , - loadArray DisplayAllButtonActionPerformed,

, sortArray , , .

+4
2

loadArray, Temp

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

+2

NullPointerException, , , .

, , , sortArray . i = list.length, IndexOutOfBoundException. - for-loop.

, ( , ).

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

. - , . .

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
}

, Reservations, -

 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  
0

Source: https://habr.com/ru/post/1620229/


All Articles