Or alternatively, just use the Java library's binary search implementation.