Complete the code that will print found if an integer search

Complete the code that will print \"found\" if an integer searchInt typed in from the keyboard is found at least once in the array named x. If the int is nowhere to be found in the array, print \"not here\". The code given creates and initializes x with random elements and reads in searchInt.

final int MAX_CAPACITY = 100;
java.util.Random ranNum = new java.util.Random();
int[] x = new int[MAX_CAPACITY];

for(int j = 0; j < x.length; j++ )
{
x[j] = Math.abs( ranNum.nextInt( ) % MAX_CAPACITY + 1 );
}
Scanner scan = new Scanner(System.in);
System.out.print( \"Enter number to search for: \" );
int searchInt = scan.nextInt( );

//complete the code

Solution

SearchNumber.java

import java.util.Scanner;


public class SearchNumber {

  
   public static void main(String[] args) {
       final int MAX_CAPACITY = 100;
       java.util.Random ranNum = new java.util.Random();
       int[] x = new int[MAX_CAPACITY];

       for(int j = 0; j < x.length; j++ )
       {
       x[j] = Math.abs( ranNum.nextInt( ) % MAX_CAPACITY + 1 );
       }
       Scanner scan = new Scanner(System.in);
       System.out.print( \"Enter number to search for: \" );
       int searchInt = scan.nextInt( );
       boolean found = false;
       for(int i=0; i<x.length; i++){
           if(x[i] == searchInt){
               found = true;
               break;
           }
       }
       if(found){
           System.out.println(\"Found\");
       }
       else{
           System.out.println(\"Not found\");
       }

   }

}

Output:

Enter number to search for: 77
Found

Enter number to search for: 2
Not found

Complete the code that will print \
Complete the code that will print \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site