import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/*
 * IndexOfAndSortExample.java
 *
 * Created on April 11, 2005, 12:16 PM
 */

/**
 *
 * @author Bill Kraynek
 */
public class IndexOfAndSortExample {
    
    /** Creates a new instance of IndexOfAndSortExample */
    public IndexOfAndSortExample() throws Exception{
        Scanner fileScanner = new Scanner(new File("src/IndexOfAndSortExample.java"));
        fileScanner.useDelimiter("[^a-zA-Z]+");
        ArrayList<String> strings = new ArrayList<String>();
        while( fileScanner.hasNext() ) {
            String nextWord = fileScanner.next();
            if( !strings.contains(nextWord) ) strings.add(nextWord);
        } // end while
        display(strings);
        while( true ) {
            String s = JOptionPane.showInputDialog("Enter search word","Scanner");
            if( s== null ) break;
            JOptionPane.showMessageDialog(null,indexOf(s,strings));
        }// end while
        bubbleSort(strings);
        display(strings);
        while( true ) {
            String s = JOptionPane.showInputDialog("Enter search word","Scanner");
            if( s== null ) break;
            JOptionPane.showMessageDialog(null,binaryIndexOf(s,strings));
        }// end while
    }
    
    int indexOf(String s, ArrayList<String> strings) {
        for(int i = 0; i < strings.size(); i++){
            if( s.equals(strings.get(i)) ) return i;
        }// end for
        return -1;
    }// end indexOf
    
    void bubbleSort( ArrayList<String> words ) {
        for( int i = 0; i < words.size(); i++ ) {
            for( int j = 0; j < words.size()-(i+1); j++ ) {
                if( words.get(j).compareTo(words.get(j+1)) > 0 ) {
                    String save = words.get(j);
                    words.set(j,words.get(j+1));
                    words.set(j+1,save);
                }// end if
            }// end for
        }// end for
    }// end bubbleSort
    
    int binaryIndexOf(String s, ArrayList<String> strings) {
        int left = 0;
        int right = strings.size() - 1;
        while( left <= right ) {
            int middle = (left + right)/2;
            if( s.compareTo(strings.get(middle)) == 0 ) return middle;
            if( s.compareTo(strings.get(middle)) < 0 ) right = middle - 1;
            if( s.compareTo(strings.get(middle)) > 0 ) left = middle + 1;
        }// end while
        return -1;
    }//end binaryIndexOf
    
    String display(ArrayList<String> strings) {
        String out = "";
        for(String s : strings) {
            out += s + "\n";
        }// end for
        JTextArea outArea = new JTextArea(out,20,30);
        JOptionPane.showMessageDialog(null,new JScrollPane(outArea));
        return out;
    }// end display
       
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        new IndexOfAndSortExample();
    }
    
}

