/* * T01n15.java -- A text message typed by the user is echoed after * each letter is replaced with the same letter in the opposite case. * That is, 'b' is replaced with 'B', 'G' is replaced with 'g', etc. * * When you run this, you type a message and then press Enter. Computers * buffer input, so even though the program is reading a character at * a time from the keyboard, the program doesn't see that there's any * input to process until the content of the buffer is passed to it. * Then it will process the available input, and wait for more to appear. * * To terminate the program, the user must type the end-of-file (EOF) * character for their OS. Under UNIX-based systems, type Ctrl-D. * Under Windows (DOS), type Ctrl-Z. */ import java.util.*; public class T01n15 { public static void main (String [] args) { char character; Scanner keyboard = new Scanner (System.in); System.out.println("This program will switch case on your " + "letters.\nNon-alphabetic letters are not " + "changed.\n\nYou will see the conversion only " + "after you press Enter or use\nCtrl-D (UNIX) " + "to terminate the program.\n"); // hasNext() is true if another 'token' can be read from // the underlying stream. while (keyboard.hasNext()) { // findInLine() is used to locate // a pattern of interest within the stream. In this case, // the "." argument says to find a single character. // The match is returned as a String. character = keyboard.findInLine(".").charAt(0); if ( Character.isLowerCase(character) ) System.out.print(Character.toUpperCase(character)); else if ( Character.isUpperCase(character) ) System.out.print(Character.toLowerCase(character)); else System.out.print(character); } } }