vendredi 20 décembre 2019

if statement to print user input was not found in array search

Trying to figure out how to implement a way when if a user puts in an integer that isn't in the array, they are told it was not found. Been looking up different ways to implement, but I'm getting nowhere on how it can fit my code, hoping a different set of eyes can help em out.

import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int[] numbers = new int[]{5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70};

        System.out.print("Enter number you want to search from Array: ");
        int userInput = scanner.nextInt();

        int returnInput = Arrays.binarySearch (numbers, userInput);
        System.out.println("Binary Search found " + userInput +
                " at position " + returnInput);
        /*
        if statement to output user input was not found in array
         */
        System.out.println("Number entered was not found.");


    }
}

Aucun commentaire:

Enregistrer un commentaire