In my program I am first searching for a value, if it exists the array position is given, if not -1 is given but when i try to return the error message in my program that the value entered could not be found therefore not deleted I receive an actual error. This may sound confusing and I can try to explain more if you do not see what I am saying by looking at my code. Can someone help?
private int search(int s){
int position = 0;
while(nums[position] != s){
if(position < size)
position++;
else
position = -1;
}
return position;
}
public void delete(int d){
int value = search(d);
if(search(d) == -1){
System.out.println("The value " + d + " was not found and cannot be deleted");
}
else
for(int n = size; n > value; n--)
nums[value] = nums[size];
}
I receive the error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Numbers.search(Numbers.java:44)
at Numbers.delete(Numbers.java:53)
at Assignment7.main(Assignment7.java:31)