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.