Sounds like a counting loop.
Let us create a class Searcher that contains a linear search method.
Searcher
The search() method will search through an array of String references looking for a target String.
search()
String
Make the linear search method a static method.
static
Here is a skeleton of the program:
public class SearchTester { // seek target in the array of strings. // return the index where target is found, or -1 if not found. public static int search( array, target ) { . . . . . . // implement linear search } public static void main ( String[] args ) { final int SIZE = 20 ; String[] strArray = new String[ SIZE ] ; // put values into strArray strArray[0] = "Boston" ; strArray[1] = "Albany" ; strArray[2] = "Detroit" ; strArray[3] = "Phoenix" ; strArray[4] = "Peoria" ; strArray[6] = "Albany" ; strArray[7] = "Houston" ; strArray[8] = "Hartford" ; // call the static search method int where = Searcher.search( strArray, "Peoria" ); if ( where >= 0 ) System.out.println("Target found in cell " + where ); else System.out.println("Target not found" ); } }
Fill in the blanks for the types of the formal parameters.