Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.

Similar presentations


Presentation on theme: "1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader."— Presentation transcript:

1 1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader class. l Preview: Catching Exceptions.

2 2 WRAPPER CLASSES l Java uses primitive types, such as int and char, for performance reasons. l However, there are times when a programmer needs to create an object representation for one of these primitive types. l Java provides a Wrapper class for each of the primitive types. All these classes are in the java.lang package: Primitive typeWrapper class booleanBoolean byteByte charCharacter shortShort intInteger longLong floatFloat doubleDouble

3 3 WRAPPER CLASSES (cont.) l Wrapper classes are used to provide constants and general methods for the primitive data types. l Converting strings into numbers l 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) ; Wrapper classParse method ByteparseByte(string) ShortparseShort(string) IntegerparseInt(string) LongparseLong(string) FloatparseFloat(string) DoubleparseDouble(string)

4 4 INTRODUCTION TO EXCEPTIONS l A program may have one or more of three types of errors: 1.Syntax errors or Compile-time errors. 2.Run-time or Execution-time errors. 3.Logic errors. l 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. l All Java exception classes are subclasses of the Throwable class. l 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. l Catching and throwing an exception l 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. l 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.

5 5 INTRODUCTION TO EXCEPTIONS (cont.) l A partial hierarchy of Java exceptions: Throwable ExceptionError... IOException RuntimeException... IllegalArgumentException NumberFormatException NullPointerException ArithmeticException...

6 6 INTRODUCTION TO EXCEPTIONS (cont.) l Checked and Unchecked exceptions Java exceptions are classified into two categories: checked exceptions and unchecked exceptions. Any exception that derives from the class Error or the class RuntimeException is an unchecked exception. All other exceptions are checked exceptions. l 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. l 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 {... }

7 7 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.

8 8 CONSOLE INPUT USING BufferedReader CLASS (cont.)  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.

9 9 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) ; l 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) ; l 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)) ; l Note: Both the BufferedReader class and the InputStreamReader class are defined in the java.io package.

10 10 CONSOLE INPUT USING BufferedReader CLASS (cont.) l 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 l 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 ) ; }

11 11 CONSOLE INPUT USING BufferedReader CLASS (cont.) l Example: Reading a character char ch = (char) stdin.read( ) ; l 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. l 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


Download ppt "1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader."

Similar presentations


Ads by Google