/* * This program asks the user to enter an integer and prints the index of that integer from an array * * @author Julia Norrish * @version v100 */ class Main { //Main public static void main(String[] args) {//main int [] list = {48, 16, 19, 47, 13, 39, 4, 11, 45, 9, 18, 6, 50, 27, 7, 43, 21, 24, 2, 17, 30, 49, 33, 22, 3,}; int target = 0; Scanner keyboard = new Scanner(System.in); System.out.print("Enter an integer > "); target = keyboard.nextInt(); System.out.println("The index of " + target + " is " + linearSearch(list, list.length, target)); }//main /* * This routine implements a sequential search for a target value * * @param list - the arry that them method is searching * @param target - the value the method is trying to find * @return the index of the target value */ public static int linearSearch (int [] list, int n, int target) {//linearSearch int index = 0; for (int i = 0; i < n; i++) {//for if(list[i] == target) {//if index = i; break; }//if else { index = -1; } }//for return index; }//linearSearch /* * This routine implements a sequential search for a target value * * @param list - the arry that them method is searching * @param target - the value the method is trying to find * @return the index of the target value */ public static int linearSearch (String[] list, int n, String target) {//linearSearch int index = 0; for (int i = 0; i < n; i++) {//for if(list[i].equals( target)) {//if index = i; break; }//if else { index = -1; } }//for return index; }//linearSearch }//Main