home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch02 / Phone.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  2.4 KB  |  83 lines

  1. import java.io.*;
  2. import java.util.StringTokenizer;
  3.  
  4. /*
  5.  * Phone.class implements a simple phone book with fuzzy
  6.  * name lookup.  The phone book file could be created with a
  7.  * text editor or a spreadsheet saved as tab-delimited text.
  8.  */
  9. class Phone {
  10.  
  11. public static void main (String args[])
  12. {
  13.         String buf;
  14.         FileInputStream fs=null;
  15.  
  16.         if (args.length != 1) {
  17.                System.out.println ("usage: java Phone <name>");
  18.                System.exit (1);
  19.         }
  20.  
  21. /*
  22.  * PhoneBook.txt is the name of the phone book file.
  23.  */
  24.        try {
  25.               fs = new FileInputStream ("PhoneBook.txt");
  26.        } catch (Exception e) {
  27.                System.out.println ("Unable to open PhoneBook.txt");
  28. System.exit (1);
  29.        }
  30.       BufferedReader ds = new BufferedReader(new InputStreamReader(fs));
  31. while (true) {
  32.                try {
  33.                       buf = ds.readLine ();
  34.                       if (buf == null) break;
  35.                } catch (IOException e) {
  36.                       System.out.println ("Exception caught reading file."); 
  37. break;
  38.                }
  39.  
  40. /*
  41.  * Create a new StringTokenizer for each line read
  42.  * Explicitly specify the delimiters as both colons and tabs
  43.  */
  44.                StringTokenizer st = new StringTokenizer(buf, ":\t");
  45.  
  46.                String name = st.nextToken ();
  47.                if (contains (name, args[0])) {
  48.                       System.out.println (name);
  49.                       System.out.println (st.nextToken ());
  50.                       System.out.println (st.nextToken () + "\n");
  51. }
  52.         }
  53.         try {
  54.               fs.close ();
  55.         } catch (IOException e) {
  56.               System.out.println ("Exception caught closing file.");
  57. }
  58. }
  59.  
  60. /*
  61.  * contains method is a fuzzy string compare that returns true
  62.  * if either string is completely contained in the other one
  63.  *      String s1, s2  two strings to compare
  64.  */
  65. static boolean contains (String s1, String s2) {
  66.  
  67.         int i;
  68.         int l1 = s1.length ();
  69.         int l2 = s2.length ();
  70.  
  71.         if (l1 < l2) {
  72.                for (i=0; i<=l2-l1; i+=1)
  73.                       if (s1.regionMatches (true, 0, s2, i, l1))
  74.                              return true;
  75.         }
  76.         for (i=0; i<=l1-l2; i+=1)
  77.                if (s2.regionMatches (true, 0, s1, i, l2))
  78.                       return true;
  79.  
  80.         return false;
  81. }
  82. }
  83.