/* Author: James Tam Version: July 12, 2006: * Changed the access level for the class. * Changed some of the documentation to better clarify how the program worked. * Added some additional error messages to make it easier to trace the output of the program. Version: October 14, 2003 (Handles exceptions) An example to illustrate how to call methods that may cause exceptions to be thrown. This version handles the exception by displaying the type of exception that was thrown and the input that caused the exception to occur. */ import java.io.*; public class Driver { public static void main (String [] args) { // System.in: Manipulates bytes // InputStreamReader: Manipulates individual characters // BufferedReader: Manipulates Strings BufferedReader stringInput; InputStreamReader characterInput; String s; int num; // Allows the input to be converted from byte input to character form. characterInput = new InputStreamReader(System.in); // Allows the input to be converted from character input to String form stringInput = new BufferedReader(characterInput); try { System.out.print("Type an integer: "); // Read a line of text from the command line, converts the raw bytes to characters, // characters to a String. s = stringInput.readLine(); System.out.println("You typed in..." + s); // Convert the String to integer format. num = Integer.parseInt(s); System.out.println("Converted to an integer..." + num); } catch (IOException e) { System.out.println("Input/output error occured"); System.out.println(e); } catch (NumberFormatException e) { System.out.println("A non-integer value was entered."); System.out.println("----------"); System.out.println("e.getMessage()"); System.out.println(e.getMessage()); System.out.println("----------"); System.out.println("System.out.println(e)"); System.out.println(e); System.out.println("----------"); System.out.println("e.printStackTrace()"); e.printStackTrace(); System.out.println("----------"); } } }