Source of BreakRecords.java


  1: /* 
  2:  * Copyright (c) 1999-2002, Xiaoping Jia.  
  3:  * All Rights Reserved. 
  4:  */
  5: 
  6: import  java.io.*;
  7: 
  8: /**
  9:  *  This program breaks colon-delimited records. 
 10:  */
 11: public class BreakRecords {
 12:   
 13:   public static void main(String[] args) {
 14:     BufferedReader in = new BufferedReader(
 15:                             new InputStreamReader(System.in));
 16:     try {
 17:       String record, field;
 18:       char delim =':';   // the delimiter
 19:       for (int n = 1; (record = in.readLine()) != null; n++) {
 20:         System.out.println("Record  " + n);
 21:         int begin, end, i;
 22:         begin = 0;
 23:         for (i = 0; (end = record.indexOf(delim, begin)) >= 0; i++) {
 24:           field = record.substring(begin, end);
 25:           begin = end + 1;  // skip the delimiter
 26:           System.out.println("\tField " + i + ": " + field);
 27:         }
 28:         field = record.substring(begin);  // the last field
 29:         System.out.println("\tField " + i + ": " + field);
 30:       }
 31:     } catch (IOException e) {}
 32:   }
 33: }