Complete Program

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 completed program is below.

Complete Program

Here is the complete program. As soon as the for loop finds the cell that contains the target, the method returns to the caller with the index of that cell. If the for loop reaches the end of the array, -1 is returned.

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 )
         if ( array[j].equals( target ) ) return j ; // Target found.

     return -1 ; // Target not found 
  }
}

class SearchTester
{
  public static void main ( String[] args )
  {
    final int theSize = 20 ;

    String[] strArray = new String[ theSize ] ;  

    strArray[0] = "Boston" ;
    strArray[1] = "Albany" ;
    strArray[2] = "Detroit" ;
    strArray[3] = "Phoenix" ;
    strArray[4] = "Peoria" ;
    strArray[6] = "Albany" ;
    strArray[7] = "Houston" ;
    strArray[8] = "Hartford" ;

    // print out only those cells with data
    for (int j=0; j  < strArray.length; j++ )
      if ( strArray[j] != null )
        System.out.println( j + ": " + strArray[j] );

    // search for "Peoria"
    int where = Searcher.search( strArray, "Peoria" ); 
    if ( where >= 0 )
      System.out.println("Target found in cell " + where );
    else
      System.out.println("Target not found" );

  }
}

QUESTION 20:

How could this program be improved?