title: Binary and linear search program to find an integer. Responds yes if it is there and no if it is not. How to get the binary search working?
body: <p>I am trying to get the binary search working. It is supposed to ask you to an array size, the integers to be inputted in the array and search the array to find a number you are looking for along with how many probes it took to find it or responds with number not found. I am following the algorithm but it either responds with an infinite loop or not at all because it runs forever. Any suggestions?</p>
  
  <pre><code>package assignment3;
  
  import java.util.Scanner;
  
  public class search{
  
      public static void main(String[] arg){
  
          Scanner range=new Scanner(System.in);
  
          System.out.println("Pick your array size.");
  
          int element=range.nextInt();
  
          int[] array=new int[element];
  
          Scanner array_list=new Scanner(System.in);
  
          if(element&lt;=0){
  
              System.out.println("The array size you chose is not supported. You must chose again");
  
              Scanner tryagain=new Scanner(System.in);
  
              System.out.println("Try again");
  
              element=tryagain.nextInt();    
          }
  
          System.out.println("Now enter all the numbers in your array");
  
          String list=array_list.nextLine();
  
          String[] newlist=list.split("\\ ");
  
          int lengthofarray=newlist.length;
  
          for(int i=0; i&lt;lengthofarray; i++){
  
              array[i]=Integer.parseInt(newlist[i]);
  
              System.out.println(i+" || "+array[i]);
  
          }
  
          Scanner linearSearch=new Scanner(System.in);
  
          System.out.println("Pick a number to see if it is in the array and how many times it took to find it.");
  
          int linear=linearSearch.nextInt();
          Scanner linear2=new Scanner(System.in);
          System.out.println("Now we will try the same thing with a binary search. Pick a number");
          int binarysearch=linear2.nextInt();
          int low=0;
          int high=lengthofarray-1;
          int middle;
  
          while(low&lt;=high){
  
              middle=(low+high)/2;
  
              if(array[middle]&gt;binarysearch){
                  high=middle-1;
              }
  
              else if(array[middle]&lt;binarysearch){
                  low=middle+1;
              }
          }
          if(array[low]==binarysearch){
              System.out.println("Your number is in the array.");
  
          }
          else{
              System.out.println("your number is not in the array.");
          }
      }
  }
  </code></pre>