Dúvida iniciante Java

Boa noite, estou em dúvida sobre o que está levando o meu programa a funcionar direito com os números 6, 8 e 9 mas não com 3 e o 4 no array.
O código juntamente com os resultados se encontra abaixo:

  int v[] = {6,3,8,4,9};
        System.out.println("Array:{6,3,8,4,9}");
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Could you say the number you would like to remove?");
        int answer = keyboard.nextInt();
        int search = Arrays.binarySearch(v, answer);
        System.out.println("Analyzing your number.....");
        if(search>=0){
            v[search] = 0;
            for(int value: v){
                System.out.print(value + " ");
            }
            System.out.println("Successfully removed!");
        }else{
            System.out.println("Sorry, but this number is not a component of this array.");
        }

Resultado com sucesso: http://prntscr.com/dffjoi
Resultado sem sucesso: http://prntscr.com/dffjr5

Obrigado.

A partir da documentação:

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#binarySearch(int[],%20int)

Searches the specified array of ints for the specified value using the binary search algorithm.** The array must be sorted (as by the sort(int[]) method)** prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.

este metodo espera que o seu array esteja ordenado.

use este metodo

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(int[])

assim


 int v[] = {6,3,8,4,9};
Arrays.sort( v ); /* vai ordenar o array */

Poxa vida! Verdade! Brigadão :slight_smile: