Hi, I am trying to write a binary search. The main method randomly makes an array of 1 mil int's, and the binary search returns if an entered int is in the array, but I seem to be having trouble getting it to work, I've been working on it for a few hours now and any input would be greatly appreciated.

package binarysearch;
import java.util.Random;
import java.util.Scanner;
public class Search {
    public static void main(String[] args) {
        int[] array = new int[1000000];
        array[0] = 1;
        Random randGen = new Random();
        for (int i = 1; i < array.length; i++) {
            array[i] = array[i - 1] + randGen.nextInt(3) + 1;
        }
        Scanner input = new Scanner (System.in);
        System.out.print("Please enter a number to be checked: ");
        int userinput = input.nextInt();
        inArray(array, userinput);
//        for (int j = 0; j < array.length; j++) {
//            System.out.println(array[j]);
//        }
        if (inArray(array, userinput) != -1) {
            System.out.println(userinput + " is in the array!");
            System.out.println(userinput + " was found in spot " + inArray(array, userinput));
        } else {
            System.out.println(userinput + " is not in the array!");
        }
    }

    public static int /*boolean*/ inArray(int[] array, int userinput) {
        int count = 0;
        int low = 0;
        int high = array.length - 1;
        boolean check = false;
        int mid = (low + high) / 2;
        while (true) {
            if (check = true) {
                count++;
                break;
            }
            if (high < low || low > high) {
                check = true;
                count++;
            }
            if (array[mid] == userinput) {
                check = true;
                count++;
            }
            if (array[mid] > userinput) {
                high = mid;
                count++;
            }
            if (array[mid] < userinput) {
                low = mid;
                count++;
            }
        }
//        }
//        return true;
        if (check == true) {
            return count;
        } else {
            return -1;
        }
    }
}

I know that my count function and what I am checking is wrong but still can't seem to figure it out...does anyone have a quick fix or is there a bigger problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.