CSE 283 Introduction to Object Oriented Design

Barbara Nostrand, Ph.D.


Terminal Oriented I/O

Before there were GUI's,  there were keyboards and displays.  Communication between the program and the user was text based.  The program would print a message called a prompt asking the user for data,  and the user would type in a response.  This note is about how to communicate with a Java program in this way.  Although you can write all of your I/O statements using Java standard classes and methods,  you may find Keyboard and Screen to be a bit friendlier. 

Class Keyboard

Constructor Summary
Keyboard()
          Creates a Keyboard object to which to attach the Keyboard methods. 

Method Summary
 chargetChar()
          Returns and removes the next char,  white space or not. 
 charpeekChar()
          Returns without removing the next char,  white space or not. 
 voidEatWhiteSpace()
          Skips over 0 or more non-printing characters. 
 charreadChar()
          Returns and removes the next printing character. 
 StringreadWord()
          Returns and removes the next unbroken sequence of non-white-space chars. 
 StringreadLine()
          Returns and removes the sequence of chars from the current read position to * the next newline. 
 booleanreadBoolean()
          Returns the boolean equivalent of the next word. 
 bytereadByte()
          Returns the the byte-integer equivalent of the next word. 
 shortreadShort()
          Returns the the short-integer equivalent of the next word. 
 intreadInt()
          Returns the integer equivalent of the next word. 
 intreadOctalInt()
          Returns the base-8 integer equivalent of the next word.. 
 intreadHexInt()
          Returns the base-16 integer equivalent of the next word.. 
 longreadLong()
          Returns the long-int equivalent of the next word. 
 floatreadFloat()
          Returns the the float equivalent of the next word. 
 doublereadDouble()
          Returns the the double equivalent of the next word. 

Class Screen

Constructor Summary
Screen()
          Creates a Screen object to which to attach the Screen methods. 

Method Summary
 Screen println()
          Sends a newline to the Screen.  Returns the Screen object. 
 Screen print(Object obj)
           displays the object obj using its toString() method.  Returns the Screen object. 
 Screen println(Object obj)
           displays the object obj using its toString() method followed by a newline.  Returns the Screen object. 
 Screen print(char c)
           Displays character c.  Returns the Screen object. 
 Screen println(char c)
           Displays character c followed by a newline.  Returns the Screen object. 
 Screen print(String s)
           Displays String s.  Returns the Screen object. 
 Screen println(String s)
           Displays String s followed by a newline.  Returns the Screen object. 
 Screen print(boolean b)
           Displays boolean b.  Returns the Screen object. 
 Screen println(boolean b)
           Displays boolean b followed by a newline.  Returns the Screen object. 
 Screen print(byte b)
           Displays byte b.  Returns the Screen object. 
 Screen println(byte b)
           Displays byte b followed by a newline.  Returns the Screen object. 
 Screen print(short s)
           Displays short s.  Returns the Screen object. 
 Screen println(short s)
           Displays short s followed by a newline.  Returns the Screen object. 
 Screen print(int x)
           Displays int x.  Returns the Screen object. 
 Screen println(int x)
           Displays int x followed by a newline.  Returns the Screen object. 
 Screen print(long x)
           Displays long x.  Returns the Screen object. 
 Screen println(long x)
           Displays long x followed by a newline.  Returns the Screen object. 
 Screen print(float x)
           Displays float x.  Returns the Screen object. 
 Screen println(float x)
           Displays float x followed by a newline.  Returns the Screen object. 
 Screen print(double x)
           Displays double x.  Returns the Screen object. 
 Screen println(double x)
           Displays double x followed by a newline.  Returns the Screen object. 
 Screen printFormatted(double x)
           Displays double x using the default format.  Returns the Screen object. 
 Screen printFormatted(double x, int decimalDigits)
           Displays double x with a given number of decimal digits.  Returns the Screen object. 
 Screen printFormatted(double x, int left, int right)
           Displays double x with a given number of places left and right of the decimal.  Returns the Screen object. 
 Screen printFormatted(double x, int left, int right, char fill)
           Displays double x with a given number of places left and right of the decimal.  Returns the Screen object. 

Using the Keyboard and Screen classes. 

Using Keyboard and Screen

// A simple demonstration of using Keyboard and Screen for terminal I/O
// We do not need to import java.io.* as all of the direct terminal I/O  
// has been exported to the Keyboard and Screen classes.

    public class Demo {
   
       public static void main (String [] args) {
     
         Screen theScreen = new Screen();
         Keyboard theKeyboard = new Keyboard();
         theScreen.println("Hello!\n");
         theScreen.print("Integer = ");
         int number = theKeyboard.readInt();
         theScreen.println("Number = " + number);
      }
   }

Using java.io directly

// A simple demonstration of using built-in classes for terminal I/O

    import java.io.*;   // provides for terminal i/o

    class Demo {
       public static void main(String [] args) throws Exception {
         BufferedReader console = 
            new BufferedReader(new InputStreamReader(System.in));
         System.out.print("number = ");
         int myNumber = Integer.parseInt(console.readLine());
         System.out.println("My number is " + myNumber);   }     
   }

Outputing Special Characters

Some characters such as single and double quote have special meanings within Java and C++ or are "non-printing".  Escape Sequences allow us to include these characters in a String and send them to our output device. 

Escape Sequences

Character NameJavaC++
Backspace\b\b
Horizontal Tab\t\t
Vertical Tab\v
Newline\n\n
Carriage Return\r\r
Form Feed\f
Alert (BEEP)\a
Question Mark\?
Double Quote\"\"
Single Quote\'\'
Backslash\\\\
Unicode Character\uhhh
ASCII Character\ooo
ASCII Character\xhh



Last modified: 2007 SEP 18
bnostran@syr.edu