Why is my code not working?

package arrays;

import java.util.Scanner;

public class binarySearch

public static void search(int[] arr,int item) {
	int low=0;
	int high=arr.length-1;
	while(low<=high) {
		int mid=(low+high)/2;
		if (arr[mid]>item) {
			high=mid-1;
		}
		else if(arr[mid]<item) {
			low=mid+1;
		}
		else if (arr[mid]==item) {
			System.out.println(mid);
		}
		else
			System.out.println("not found");
	}
}
public static void main(String[] args) {
	int[] arr= {4,6,3,8,7,9};
	System.out.println("enter the element to be searched\n");
	Scanner s=new Scanner(System.in);
	int n=s.nextInt();
	search(arr,n);
}

}

@laibaahsan27_1dfa992390072fd9 Your code will run for infinite times bcoz when you found the element you need to break the loop after printing the index. Otherwise the loop will continue to iterate.
Corrected Code :

package arrays;

import java.util.Scanner;

public class binarySearch

public static void search(int[] arr,int item) {
	int low=0;
	int high=arr.length-1;
	while(low<=high) {
		int mid=(low+high)/2;
		if (arr[mid]>item) {
			high=mid-1;
		}
		else if(arr[mid]<item) {
			low=mid+1;
		}
		else if (arr[mid]==item) {
			System.out.println(mid);
                        break;
		}
		else
			System.out.println("not found");
	}
}
public static void main(String[] args) {
	int[] arr= {4,6,3,8,7,9};
	System.out.println("enter the element to be searched\n");
	Scanner s=new Scanner(System.in);
	int n=s.nextInt();
	search(arr,n);
}
}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.