Source of LinearSearchTest.java


  1: // Fig 16.3.: LinearSearchTest.java
  2: // Sequentially search an array for an item.
  3: import java.util.Scanner;
  4: 
  5: public class LinearSearchTest
  6: {
  7:    public static void main( String args[] )
  8:    {
  9:       // create Scanner object to input data
 10:       Scanner input = new Scanner( System.in );
 11: 
 12:       int searchInt; // search key
 13:       int position; // location of search key in array
 14: 
 15:       // create array and output it
 16:       LinearArray searchArray = new LinearArray( 10 );
 17:       System.out.println( searchArray ); // print array
 18: 
 19:       // get input from user
 20:       System.out.print( 
 21:          "Please enter an integer value (-1 to quit): " );
 22:       searchInt = input.nextInt(); // read first int from user
 23: 
 24:       // repeatedly input an integer; -1 terminates the program
 25:       while ( searchInt != -1 )
 26:       {
 27:          // perform linear search
 28:          position = searchArray.linearSearch( searchInt );
 29: 
 30:          if ( position == -1 ) // integer was not found
 31:             System.out.println( "The integer " + searchInt + 
 32:                " was not found.\n" );
 33:          else // integer was found
 34:             System.out.println( "The integer " + searchInt + 
 35:                " was found in position " + position + ".\n" );
 36: 
 37:          // get input from user
 38:          System.out.print( 
 39:             "Please enter an integer value (-1 to quit): " );
 40:          searchInt = input.nextInt(); // read next int from user
 41:       } // end while
 42:    } // end main
 43: } // end class LinearSearchTest
 44: 
 45: 
 46: /**************************************************************************
 47:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 48:  * Pearson Education, Inc. All Rights Reserved.                           *
 49:  *                                                                        *
 50:  * DISCLAIMER: The authors and publisher of this book have used their     *
 51:  * best efforts in preparing the book. These efforts include the          *
 52:  * development, research, and testing of the theories and programs        *
 53:  * to determine their effectiveness. The authors and publisher make       *
 54:  * no warranty of any kind, expressed or implied, with regard to these    *
 55:  * programs or to the documentation contained in these books. The authors *
 56:  * and publisher shall not be liable in any event for incidental or       *
 57:  * consequential damages in connection with, or arising out of, the       *
 58:  * furnishing, performance, or use of these programs.                     *
 59:  *************************************************************************/