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];
}
public void addArrayItem(int index, Reservations reservation) {
this.reservationsArray[index] = reservation;
}
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;
}
}
return maxIndex;
}
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;
}
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 , , .