/* This is a program that searches for a target through an array and prints all the positions where it is found. See Array Problems part (b). Laura Toma csci107 */ public class SearchArray { public static void main (String args[]) { //set up the variable that reads from user ReadStream r = new ReadStream(); //declare an array of elements and initialize it int a[] = {3,5,6,2,9,2,1,4,6,5,3}; //print the elements in the array to the user System.out.println("The array is: "); int i = 0; while (i < a.length) { System.out.print(a[i] + " "); i = i+1; } System.out.println(); //read the target from user System.out.println("Enter target to search: "); int target = r.readInt(); r.readLine(); //search for target in the array int count = 0; //nb of occurences of target in the array i = 0; while (i < a.length) { if (a[i] == target) { System.out.println("Target found at position " + i); count = count +1; } i = i+1; } if (count == 0) System.out.println("Target not found"); } // end of main } // end of class