Source of PrintCapsOnly.java


  1: //PrintCapsOnly.java
  2: //Reads in up to a full line of characters and then writes
  3: //out only those which are capitals, in the same order.

  5: import java.io.IOException;

  7: public class PrintCapsOnly
  8: {
  9:     public static void main(String[] args) throws IOException
 10:     {
 11:         System.out.println("\nThis program reads in up to a full line of "
 12:             + "characters and then writes out the\ncapitals only, in the "
 13:             + "same order they were entered.\n");

 15:         System.out.print("Enter the line of characters on the line below:\n");
 16:         readCharsAndPrintCapsOnly();
 17:         System.out.println();
 18:     }

 20:     public static void readCharsAndPrintCapsOnly() throws IOException
 21:     /**<
 22:     Read a line of characters and print out only those that are
 23:     capital letters, in the same order that they were entered.
 24:     <p>Pre:<p>The input stream is empty.
 25:     <p>Post:<p>A line of characers has been entered and the capital
 26:     letters in that line have been written out in their original order.
 27:     */
 28:     {
 29:         char ch = (char)System.in.read();
 30:         if (ch != '\n')
 31:         {
 32:             if (ch >= 'A' && ch <= 'Z') System.out.print(ch);
 33:             readCharsAndPrintCapsOnly();
 34:         }
 35:     }
 36: }