Reputation: 29
Is there anything wrong with my code structure wise that is causing me to get errors? I think I fixed my for loops and the parameters and everything is fine now but I am still getting errors?
public static int indexOf(int[] list, int searchValue) throws BadArrayException
{
int indexValue = 0;
if(list == null)
throw new BadArrayException("Array is null");
else if((list.length) == 0)
return -1;
for(int i = 0; i < (list.length); i++){
if(list[i] == searchValue)
indexValue = i;
}
return indexValue;
}
public static int lastIndexOf(int[] list, int searchValue) throws BadArrayException
{
int indexValue = 0;
int last = (list.length-1);
if(list.length == 0)
return -1;
for(int i = last; i >= 0; i--){
if(list[i] == searchValue)
indexValue = i;
}
return indexValue;
}
}
What I am getting:
Testing ArrayUtils class exception handling - 2/07/2014
--- Testing minValue method ---
Getting minVal of a null array OK - minValue threw exception for null array: BadArrayException
Getting minVal of an empty array OK - minValue threw exception for empty array: BadArrayException
Getting minVal() of: [10] OK - expected minValue to return 10 and got: 10
Getting minVal() of: [20,30] OK - expected minValue to return 20 and got: 20
Getting minVal() of: [40,30] OK - expected minValue to return 30 and got: 30
Getting minVal() of: [10,10] OK - expected minValue to return 10 and got: 10
Getting minVal() of: [15,25,35] OK - expected minValue to return 15 and got: 15
Getting minVal() of: [25,20,30] OK - expected minValue to return 20 and got: 20
Getting minVal() of: [50,40,30] OK - expected minValue to return 30 and got: 30
Getting minVal() of: [50,-10,40,-25,50,40,30] OK - expected minValue to return -25 and got: -25
Getting minVal() of: [200,50,-40,60,-15,30,75] OK - expected minValue to return -40 and got: -40
--- Testing copyRange method ---
Getting copyRange of a null array OK - copyRange threw exception for null array: BadArrayException
Getting copyRange(list,-1) of: [] OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1
Getting copyRange(list,0) of: [] OK - copyRange copied the array correctly as []
Getting copyRange(list,1) of: [] OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
Getting copyRange(list,-1) of: [20] OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1
Getting copyRange(list,0) of: [20] OK - copyRange copied the array correctly as [20]
Getting copyRange(list,1) of: [20] OK - copyRange copied the array correctly as []
Getting copyRange(list,2) of: [20] OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 2
Getting copyRange(list,-1) of: [10,15] OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1
Getting copyRange(list,0) of: [10,15] OK - copyRange copied the array correctly as [10,15]
Getting copyRange(list,1) of: [10,15] OK - copyRange copied the array correctly as [15]
Getting copyRange(list,2) of: [10,15] OK - copyRange copied the array correctly as []
Getting copyRange(list,3) of: [10,15] OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 3
Getting copyRange(list,-1) of: [30,35,40,45,50] OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1
Getting copyRange(list,0) of: [30,35,40,45,50] OK - copyRange copied the array correctly as [30,35,40,45,50]
Getting copyRange(list,1) of: [30,35,40,45,50] OK - copyRange copied the array correctly as [35,40,45,50]
Getting copyRange(list,2) of: [30,35,40,45,50] OK - copyRange copied the array correctly as [40,45,50]
Getting copyRange(list,3) of: [30,35,40,45,50] OK - copyRange copied the array correctly as [45,50]
Getting copyRange(list,4) of: [30,35,40,45,50] OK - copyRange copied the array correctly as [50]
Getting copyRange(list,5) of: [30,35,40,45,50] OK - copyRange copied the array correctly as []
Getting copyRange(list,6) of: [30,35,40,45,50] OK - copyRange threw exception java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 6
--- Testing indexOf and lastIndexOf method ---
Getting indexOf of a null array OK - indexOf threw exception for null array: BadArrayException
Getting lastIndexOf of a null array ERROR - lastIndexOf threw an unexpected exception: java.lang.NullPointerException
Getting indexOf(5) of: [] OK - expected indexOf to return -1 and got: -1
Getting lastIndexOf(5) of: [] OK - expected lastIndexOf to return -1 and got: -1
Getting indexOf(20) of: [20] OK - expected indexOf to return 0 and got: 0
Getting indexOf(25) of: [20] ERROR - expected indexOf to return -1 but got: 0
Getting lastIndexOf(20) of: [20] OK - expected lastIndexOf to return 0 and got: 0
Getting lastIndexOf(25) of: [20] ERROR - expected lastIndexOf to return -1 but got: 0
Getting indexOf(5) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected indexOf to return 0 but got: 6
Getting indexOf(10) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected indexOf to return 1 but got: 7
Getting indexOf(15) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected indexOf to return 2 but got: 8
Getting indexOf(20) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected indexOf to return 3 but got: 9
Getting indexOf(0) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected indexOf to return -1 but got: 0
Getting lastIndexOf(5) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected lastIndexOf to return 6 but got: 0
Getting lastIndexOf(10) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected lastIndexOf to return 7 but got: 1
Getting lastIndexOf(15) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected lastIndexOf to return 8 but got: 2
Getting lastIndexOf(20) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected lastIndexOf to return 9 but got: 3
Getting lastIndexOf(0) of: [5,10,15,20,10,15,5,10,15,20] ERROR - expected lastIndexOf to return -1 but got: 0
Done - press enter key to end program
THESE SHOULD all be OK not errors Am I approaching this code incorrectly? Sorry I asked about this before but not in the correct manner
Upvotes: 0
Views: 109
Reputation: 23
The code have three problems,
list
at lastIndexOf
function, is missing. If you aggregate that validation you will avoid the error:ERROR - lastIndexOf threw an unexpected exception: java.lang.NullPointerException
indexValue
with -1
instead of 0
, to avoid:ERROR - expected indexOf to return -1 but got: 0
searchValue
you must return the index i
. With this change you will avoid errors like:ERROR - expected indexOf to return 2 but got: 8
With this three changes your code must look like this:
public static int indexOf(int[] list, int searchValue) throws BadArrayException
{
int indexValue = -1;
if(list == null)
throw new BadArrayException("Array is null");
//Not neccessary
/*else if((list.length) == 0)
return -1;*/
for(int i = 0; i < (list.length); i++){
if(list[i] == searchValue)
return i;
}
return indexValue;
}
public static int lastIndexOf(int[] list, int searchValue) throws BadArrayException
{
int indexValue = -1;
if(list == null)
throw new BadArrayException("Array is null");
int last = (list.length-1);
//Not neccessary
/*if(list.length == 0)
return -1;*/
for(int i = last; i >= 0; i--){
if(list[i] == searchValue)
return i;
}
return indexValue;
}
Notice that some validation you made in your code are not anymore necessary if indexValue
is initialized to -1
.
Finally I recommend you to use some debugging tools or run your code with paper and pen, to discover the reasons of errors.
Hope to help you. Regards!
Upvotes: 1
Reputation: 46
when you find the value, you should break from the loop:
for(int i = 0; i < (list.length); i++){
if(list[i] == searchValue) {
indexValue = i;
**break;**
}
}
Upvotes: 0
Reputation: 393821
One thing you are missing is null check of the array in lastIndexOf
.
But the main error is that you don't break out of the loop once you find the first match (this is true for both methods).
Oh, and int indexValue = 0;
is wrong. You should initialize it to -1 (since that's the value that indicates "not found").
Taking all these things into account, I think this should work :
public static int indexOf(int[] list, int searchValue) throws BadArrayException
{
int indexValue = -1;
if(list == null)
throw new BadArrayException("Array is null");
else if((list.length) == 0)
return -1;
for(int i = 0; i < (list.length); i++){
if(list[i] == searchValue) {
indexValue = i;
break;
}
}
return indexValue;
}
public static int lastIndexOf(int[] list, int searchValue) throws BadArrayException
{
int indexValue = -1;
if(list == null)
throw new BadArrayException("Array is null");
int last = (list.length-1);
if(list.length == 0)
return -1;
for(int i = last; i >= 0; i--){
if(list[i] == searchValue) {
indexValue = i;
break;
}
}
return indexValue;
}
}
Upvotes: 2