Question & Answer: Write code to search an integer array named details for the value in the variable named findMe. The array has num number of el…..

Write code to search an integer array named details for the value in the variable named findMe. The array has num number of elements in the list. Inside the loop, print out the element at each index separated with a comma, and stop searching the array once you find the value in the variable findMe. Note: if the value in findMe is not in the list, then you will print out each element in the array.

For example, if the array has {1, 2, 3} and findMe is 2, you would print:

1,2

Expert Answer

 

The below code satisfies the given requirements as follows:-

——————————————————————

import java.util.Scanner;

public class SearchNum{

public static void main(String args[]){

int num,findMe; //initializing num and findMe variables

Scanner sc=new Scanner(System.in); //Scanner class to read from the keyboard

System.out.println(“Enter the length of the integer array”);

num=sc.nextInt(); //length of the array

System.out.println(“Enter the elements into the integer array”);

for (int i = 0; i < num; i++)

details[i] = sc.nextInt(); //loop to read the elements into the array

System.out.println(“Enter the element to find in the integer array”);

findMe=sc.nextInt(); //reading findMe variable

System.out.println(“Required ouput : “);

System.out.print(“{“+details[0]); //to print the elements with comma seperated initially printing the first element of array

if(details[0]!=findMe) //if findMe is the first element then it does not enter into the loop using if condition

{

for(int i=1;i<details.length;i++)

{

System.out.print(“,”+details[i]);

if(details[i]==findMe) //if we found findMe variable then exiting from the loop using break

{

break;

}

}

}

System.out.print(“}”);

}

}

Output:-

—————

Enter the length of the integer array

4

Enter the elements into the integer array

1 2 3 4

Enter the element to find in the integer array

3

Required ouput :

{1,2,3}

Still stressed from student homework?
Get quality assistance from academic writers!