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
 12:         (
 13:             "\nThis program reads in up to a full line of "
 14:             + "characters and then writes out the\ncapitals only, in the "
 15:             + "same order they were entered.\n"
 16:         );

 18:         System.out.print("Enter the line of characters on the line below:\n");
 19:         readCharsAndPrintCapsOnly();
 20:         System.out.println();
 21:     }

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