here is my code
public class BinarySearch { public static int binsearch(int key, int[] a) { int lo = 0; int hi = a.length - 1; while (lo < hi) { int mid = (lo + hi) >> 1; key < a[mid] ? hi = mid : lo = (mid + 1); } return lo--; } } i got an error when compiling
Exception in thread "main" java.lang.Error: Unresolved compilation problems: Syntax error on tokens, Expression expected instead Syntax error on token "]", delete this token Syntax error, insert "]" to complete Expression and if i change '<' to '>' as
key > a[mid] ? hi = mid : lo = (mid + 1); got a total different error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error on token ">", -> expected I am really confused about the ternary operator usage in java. after all, this code works fine in c++