The Java Program: GroupMain.java

  1 import java.util.regex.*;
  2 import java.io.*;
  3 
  4 public class GroupMain {
  5 
  6    // complex numbers of the form 񸙭i
  7    private static final String c_re ="([-+]?[0-9]+)([-+][0-9]+)i";
  8 
  9    // strings of the form "..."
 10    private static final String s_re ="\"([^\"]*)\"";
 11 
 12    // phone numbers of the form (999) 999-9999
 13    private static final String p_re =
 14       "\\(([0-9]{3})\\) ([0-9]{3})-([0-9]{4})";
 15 
 16    // lines with entries separated by white space, with possible
 17    // leading white space, and trailing junk
 18    private static final String string =
 19       "\\s*"+c_re+"\\s+"+s_re+"\\s+"+p_re+"\\s*";
 20 
 21    private static final Pattern pattern = Pattern.compile (string);
 22 
 23    public static void main (final String[] args) throws IOException {
 24 
 25       final BufferedReader reader =
 26          new BufferedReader (new InputStreamReader (System.in));
 27 
 28       // Read standard input stream line by line
 29       while (true) {
 30          final String line = reader.readLine();// get next line
 31          if (line==null) break;                // exit when end-of-stream
 32          final Matcher matcher = pattern.matcher(line);
 33          final boolean matchFound = matcher.find();
 34          if (matchFound) {
 35             final String re = matcher.group(1); // real part
 36             final String im = matcher.group(2); // imaginary part
 37             System.out.println (re + ", " + im + " i");
 38 
 39             System.out.println ("'"+matcher.group(3)+"'");
 40 
 41             final String area = matcher.group(4); // area code
 42             final String exch = matcher.group(5); // exchange
 43             final String numb = matcher.group(6); // number
 44             System.out.println (area + exch + numb);
 45          } else {
 46             System.out.println ("Error: " + line);       // write line
 47          }
 48       }
 49    } 
 50 }