Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.

Similar presentations


Presentation on theme: "1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered."— Presentation transcript:

1 1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered Reader Object  Reading Strings  Numeric Input

2 2 Primitive Type 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 primitive type. All these classes are in the java.lang package: Primitive typeWrapper class charCharacter doubleDouble floatFloat intInteger longLong   Wrapper classes are used to provide constants and general methods for the primitive data types.

3 3 String to Number Conversion  Each of the Wrapper classes Double, Float, Integer, and Long has a method to convert the string representation of a number of the corresponding primitive type into its numeric format: Wrapper classparse method DoubleparseDouble(string) FloatparseFloat(string) IntegerparseInt(string) LongparseLong(string)   Examples: int numStudents = Integer.parseInt(“500”); String inputLine = “3.5”; double studentGPA = Double.parseDouble(inputLine);

4 4 System.in stream  In Java I/O is handled by streams.  An input stream is an object that takes data from an input source and delivers that data to a program.  An output stream is an object that takes data from a program and delivers it to an output destination. [ e.g., System.out that corresponds to the monitor]  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., it corresponds to the keyboard).  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].

5 5 Wrapping System.in in a BufferedReader Object  To be able to read characters, strings, or numbers, System.in must be wrapped in other objects.  To turn System.in into a Reader object (i.e., an object that is capable of reading one character at a time), wrap it 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 Buffered Reader object: BufferedReader stdin = new BufferedReader(reader);  The steps of turning System.in into a Buffered Reader object can be combined into a single statement: BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));  Note: Both the Buffered Reader class and the InputstreamReader class are defined in the java.io package.

6 6 Reading Strings  A Buffered Reader object contains a readLine method that reads one input line at a time, and returns that line as a string.  The reading of input may cause an error. Input errors are called lOExceptions.  A programmer may write code to handle lOExceptions or he must declare that lOExceptions are not handled by a method by using the clause: throws lOException in the method header.  Example: 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); }

7 7 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.  The string is then converted to its corresponding numeric value using the parse method of an appropriate Wrapper class.  Examples: String inputLine = stdin.readLine(); int numStudents = Integer.parseInt(inputLine); double speed = Double.parseDouble(stdin.readLine()); floatHeight = Float.parseFloat(stdin.readLine().trim()); import java.io.*; public class ReadIntegers{ public static void main(String[] args)throws IOException{ BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter two integers on separate lines:”); int num1 = Integer.parseInt(stdin.readLine()); int num2 = Integer.parseInt(stdin.readLine()); System.out.println(“Sum = “ + (num1 + num2)); }


Download ppt "1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered."

Similar presentations


Ads by Google