110 likes | 216 Views
Reading Input. Overview Introduction to Wrapper classes. Introduction to Exceptions (Java run-time errors). Console input using the BufferedReader class. . WRAPPER CLASSES. Java uses primitive types, such as int and char, for performance reasons.
E N D
Reading Input Overview • Introduction to Wrapper classes. • Introduction to Exceptions (Java run-time errors). • Console input using the BufferedReader class.
WRAPPER CLASSES • Java uses primitive types, such as int and char, for performance reasons. • However, there are times when a programmer needs to create an object representation for one of these primitive types. • Java provides a Wrapper class for each of the primitive types. All these classes are in the java.lang package:
WRAPPER CLASSES (cont.) • Wrapper classes are used to provide constants and general methods for the primitive data types. • Converting strings into numbers • Each of the Wrapper classes Byte, Short, Integer, Long, Float, and Double has a method to convert a string representation of a number of the corresponding primitive type into its numeric format: Examples: int numStudents = Integer.parseInt(“500”) ; String inputLine ; . . . double studentGPA = Double.parseDouble( inputLine) ;
INTRODUCTION TO EXCEPTIONS • A program may have one or more of three types of errors: • Syntax errors or Compile-time errors. • Run-time or Execution-time errors. • Logic errors. • A Java exception is an object that describes a run-time error condition that has occurred in a piece of Java code or in the Java run-time System. • All Java exception classes are subclasses of the Throwable class. • Most exception classes are defined in the java.io and java.lang packages. other packages like: java.util, java.awt, java.net, java.text also define exception classes. • Catching and throwing an exception • A piece of Java code containing statements to handle an exception is said to catch the exception; otherwise it is said to throw that exception. • An exception that is not caught by any portion of a Java program will ultimately be caught by the default exception handler. The default exception handler displays a string describing the exception.
INTRODUCTION TO EXCEPTIONS (cont.) • A partial hierarchy of Java exceptions: Throwable Exception Error . . . . . . IOException RuntimeException . . . . . . IllegalArgumentException NullPointerException NumberFormatException ArithmeticException
INTRODUCTION TO EXCEPTIONS (cont.) • Checked and Unchecked exceptions Java exceptions are classified into twocategories:checked exceptionsandunchecked exceptions. Any exception that derives from the class Error or the class RuntimeException is an unchecked exception. All other exceptions are checked exceptions. • The Java rule for thrown exceptions: A METHOD MUST DECLARE ALL CHECKED EXCEPTIONS IT MAY THROW, OTHERWISE THE JAVA COMPILER WILL ISSUE AN ERROR MESSAGE. • The throws clause A method declares that it may throw an exception by a throws clause in the method header: access-specifier return-type method-name(parameter-list) throws exception-list { . . . }
INTRODUCTION TO EXCEPTIONS (cont.) Example: import java.io.* ; public static void main(String[ ] args) throws IOException { . . . } One or more statements that may throw an IOException that is not handled. Note: When a method declares that it throws an exception, then it may throw an exception of that class or any of its subclasses.
CONSOLE INPUT USING BufferedReader CLASS • In Java I/O is handled by streams. • Input stream An input stream is an object that takes data from an input source and delivers that data to a program. • Output stream An output stream is an object that delivers data to an output destination. • In Java, console input is usually accomplished by reading from the input stream System.in of the class java.lang.System • System.in represents the standard input stream (i.e., the keyboard). • Unfortunately, System.in has no methods for reading characters, strings, or numbers. It has a read method to read a single byte at a time.[Java uses Unicode in which each character is two bytes] • To be able to read characters, strings, or numbers, System.in must be wrapped in other objects.
CONSOLE INPUT USING BufferedReader CLASS (cont.) • To turn System.in into a Reader object (i.e., an object that is capable of reading one character at a time), wrap System.in in an InputStreamReader object: InputStreamReader reader = new InputStreamReader(System.in) ; • To turn the object referenced by reader into an object with the ability to read entire lines at a time, wrap the object in a BufferedReader object: BufferedReader stdin = new BufferedReader(reader) ; • The steps of turning System.in into a BufferedReader object can be combined into a single statement: BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)) ; • Note: Both the BufferedReader class and the InputStreamReader class are defined in the java.io package.
CONSOLE INPUT USING BufferedReader CLASS (cont.) • The read( ) and readLine( ) methods The object to which stdin refers to contains a read( ) method that reads one character at a time, and returns its integer code in the range 0 to 65535: int read( ) throws IOException It also contains a readLine( ) method that reads one input line at a time, and returns it as a string: String readLine( ) throws IOException • Example: Reading a string import java.io.*; public class ReadString { public static void main(String[ ] args) throws IOException { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)) ; System.out.println(“Enter a line of text:”) ; String message = stdin.readLine( ) ; System.out.println(“You entered: ” + message ) ; } }
CONSOLE INPUT USING BufferedReader CLASS (cont.) • Example: Reading a character char ch = (char) stdin.read( ) ; • Numeric input The Java library contains no classes to read numbers directly. One way of processing numeric input is to read it as a string using the readLine( ) method then convert it to its corresponding numeric value, using the parse method of an appropriate Wrapper class. • Example: String inputLine = stdin.readLine( ) ; int numStudents = Integer.parseInt(inputLine) ; double speed = Double.parseDouble(stdin.readLine( )) ; float height = Float.parseFloat(stdin.readLine( ).trim( )); Note: Each parse method can throw an unchecked NumberFormatException