/* This is a program that repeatedly asks the user for a target and searches the target through an array and prints all the positions where it is found. The program stops when the user enters 'n'. Laura Toma csci107 */ public class RepSearchArray { 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(); char doit = 'y'; while (doit == 'y') { //read the target from user System.out.print("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; } System.out.println("Total " + count + " times."); //ask the user if he want sto continue System.out.println(); System.out.print("Again? [y/n]: "); doit = r.readChar(); r.readLine(); } //ends of while loop System.out.println("OK, bye."); } // end of main } // end of class