// Fig. 5.14: LinearSearch.java
// Linear search of an array
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class LinearSearch extends Applet
             implements ActionListener {
   int a[];
   int element;
   String searchKey;
   Label enterLabel, resultLabel;
   TextField enter, result;
   
   public void init()
   {
      a = new int[ 100 ];

      for ( int i = 0; i < a.length; i++ ) // create data
         a[ i ] = 2 * i;

      enterLabel = new Label( "Enter integer search key" );
      add( enterLabel );

      enter = new TextField( 10 );
      enter.addActionListener( this );
      add( enter );

      resultLabel = new Label( "Result" );
      add( resultLabel );

      result = new TextField( 25 );
      result.setEditable( false );
      add( result );
   }

   public int linearSearch( int key ) 
   {   
      for ( int n = 0; n < a.length; n++ ) 
         if ( a[ n ] == key )
            return n;

      return -1;
   }

   public void actionPerformed( ActionEvent e )
   {
      searchKey = e.getActionCommand();
      element =
         linearSearch( Integer.parseInt( searchKey ) );

      if ( element != -1 )
         result.setText( "Found value in element " +
                         element );
      else
         result.setText( "Value not found" );
   }
}