Search Loop

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

The blanks are filled below.

Search Loop

The type String[] of the formal parameter array says that an array of String references is expected, but does not say how long the array is. Also notice how this static method is used in main().

Continue developing the program. The for loop will look through the cells of the array one by one starting at cell 0. Fill in the first blank so that it does this.

class Searcher
{
  // seek target in the array of strings.
  // return the index where found, or -1 if not found.
  public static int search( String[] array, String target )
  {
     for ( int j=0; j   array.length; j++ )
       if ( array[j]  null )
         // do something here with a non-null cell
  }
}

class SearchTester
{
  public static void main ( String[] args )
  {
    . . . . . .
    int where = Searcher.search( strArray, "Peoria" );
    . . . . . .
  }
}

However, unless it is full, not all cells of the array will contain a String reference. cells that contain null must be skipped. Fill in the second blank to do this.

QUESTION 17:

Fill in the blanks.