Need help reviewing my Binary Search program
<p>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 - </p>

<p>When I run my code - this is the output - </p>

<pre><code>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
</code></pre>

<p>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 - </p>

<pre><code>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)
</code></pre>

<p>Here is the code -    </p>

<pre><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 &lt; 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 &gt;= 0 &amp;&amp; i &lt; test.length;) {
            if (keyElement == test[i]) {
                flag = false;
                break;
            } else if (keyElement &gt; test[i]) {
                i++;
                if (keyElement == test[i]) {
                    flag = false;
                } else {
                    flag = true;
                }
            } else if (keyElement &lt; 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;
    }
}
</code></pre>

<p>Request someone to please guide me if I am overlooking something thats an obvious error?</p>

<p>Also, there are several possible duplicates for this post - <a href="http://stackoverflow.com/questions/12890065/binary-search-in-java">Binary search in java</a>, <a href="http://stackoverflow.com/questions/12890065/binary-search-in-java">Binary search in java</a> etc. I just need help with reviewing my code, not in implementing the program :)</p>
