I am creating an array that stores rental properties in a sorted order. Firstly alphabetical and if towns match then it should be lowest value rent placed first. The method runs fine when I only check for the alphabetical order but when I try to add in to check the rent I always get the out of bounds exception (ArrayIndexOutOfBoundsException: -1) at line 62 which is
if(this.propertyList[location].getTown().compareToIgnoreCase(town)==0){
and I dunno why.
public void insert(String reference, String town, Integer rent, Integer bedrooms) {
Integer location = propertyListSize-1;//start at end of array
while (location >=0 && this.propertyList[location].getTown().compareToIgnoreCase(town) > 0) {
this.propertyList[location + 1] = this.propertyList[location];//bump item up to next location
location = location - 1;//move to next location
if(this.propertyList[location].getTown().compareToIgnoreCase(town)==0){
if(this.propertyList[location].getRent().compareTo(rent)>0)/*rent bigger than new*/{
this.propertyList[location + 1] = this.propertyList[location];//bump item up to next location
}
}
}
this.propertyList[location + 1] = new Residential(reference, town, rent, bedrooms);
this.propertyListSize++;
}