In this activity, you will work with a loop that searches for a given value. Onc
ID: 650356 • Letter: I
Question
In this activity, you will work with a loop that searches for a given value. Once the value is found. the loop will print ?Found? along with the index of the location of the value. Create a new file. add the following code and save as file Arra3.java. Compile and run your program and verify that it runs correctly. In response to the prompt enter S. It should print Found at index 1 and Found at index 3 since the target value of 4 occurs twice - once at index 1 and again at index 3. Note that if you enter a value that is not in the array, nothing prints. The Bonus can be used to take care of that issue.Explanation / Answer
import java.util.Scanner;
public class Array2
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
int[] myArray = {1,4,9,4,8};
System.out.println("Enter a value to look for ");
int target = scan.nextInt();
boolean found =false;
for(int i=0;i<myArray.length && found==false; i++)
{
if(myArray[i] == target)
{
found = true;
}
}
if(found == false)
{
System.out.println("Target value was not found ");
}
else
System.out.println("Target value found ");
}
}