Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the code that will print \"found\" if an integer searchInt typed in fro

ID: 3920981 • Letter: C

Question

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

Explanation / Answer

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