I am trying to implement a binary search in Java, but I am having some issues with my code. It works if the element I am looking for exists in the array. If it doesn't, the program doesn't print an error message. What I mean here is -
When I run my code - this is the output -
Please enter array size
2
Please enter element 0
3
Please enter element 1
4
Sorted array elements[3, 4]
Please enter the element you want to find in the array
3
Match 3 found at index 0
If however, I look for an element that doesn't exist in the array, the program doesn't enter the else loop, and print an error message - It instead does this -
Please enter array size
2
Please enter element 0
3
Please enter element 1
4
Sorted array elements[3, 4]
Please enter the element you want to find in the array
2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at arraysPract.BinarySearch.findElement(BinarySearch.java:82)
at arraysPract.BinarySearch.main(BinarySearch.java:54)
Here is the code -
package arrays;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class BinarySearch {
static int[] binaryArr = null;
public static void main(String[] args) {
BufferedReader br = null;
String size = "";
try {
System.out.println("Please enter array size");
br = new BufferedReader(new InputStreamReader(System.in));
size = br.readLine();
if (size == null) {
System.out.println("Size can't be null");
return;
}
int ipSize = Integer.parseInt(size);
binaryArr = new int[ipSize];
String entry = "";
for (int i = 0; i < ipSize; i++) {
System.out.println("Please enter element " + i);
entry = br.readLine();
if (entry == null) {
System.out.println("Value can't be null");
return;
}
int arrEntry = Integer.parseInt(entry);
binaryArr[i] = arrEntry;
}
Arrays.sort(binaryArr);
System.out.println("Sorted array elements" + Arrays.toString(binaryArr));
System.out.println("\n");
System.out.println("Please enter the element you want to find in the array");
String findArrayElement = br.readLine();
int find = Integer.parseInt(findArrayElement);
boolean elementExists = Arrays.asList(binaryArr).contains(find);
if (elementExists==false) {
findElement(binaryArr, find);
}
else {
System.out.println("Element does not exist. Please try again");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static int findElement(int[] test, int keyElement) {
boolean flag = true;
int i = 0;
for (i = test.length / 2; i >= 0 && i < test.length;) {
if (keyElement == test[i]) {
flag = false;
break;
} else if (keyElement > test[i]) {
i++;
if (keyElement == test[i]) {
flag = false;
} else {
flag = true;
}
} else if (keyElement < test[i]) {
i--;
if (keyElement == test[i]) {
flag = false;
} else {
flag = true;
}
}
}
if (flag == false) {
System.out.println("Match " + keyElement + " found at index " + i);
}
return i;
}
}
Request someone to please guide me if I am overlooking something thats an obvious error?
Also, there are several possible duplicates for this post - Binary search in java, Binary search in java etc. I just need help with reviewing my code, not in implementing the program :)