Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.

Similar presentations


Presentation on theme: "1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on."— Presentation transcript:

1 1 Java Console I/O Introduction

2 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on Java classes –Classes need to be imported –We have just covered these concepts Many Java books do not do a good job of covering the concepts of I/O

3 3 Java I/O Approach –We will use the buffered I/O classes –We will also use the Java wrapper classes We will use these to read strings from the keyboard and then convert the string value entered to the desired data type.

4 4 Author’s Input Approach Text input method char input; input = (char) System.in.read( ); System.in.read( ) reads a character and returns an integer value (Unicode value) Cast to (char) converts integer value to character value Character value assigned to input

5 5 Our Input Approach We will use a more complex but more flexible approach to input static BufferedReader keyboard = new BufferedReader(new InputStreamReader (System.in)); String name; name = keyboard.readLine( ); Use BufferedReader methods Result is that strings are read from the keyboard

6 6 Buffered Reader Hierarchy System.in - inputs integer Unicode values InputStreamReader - takes integer values and converts them to characters BufferedReader - takes the character values and buffers them This gives us the string input

7 7 Input Example public class InputExample { static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[ ] args) throws IOException { String input; input = keyboard.readLine( ); }

8 8 Problems If all we want to input are strings we are ok However what about other data types? intfloat double For these we use wrapper classes Wrapper classes take strings and primitives as input and allows us to convert to our desired data type They wrap a primitive data type into an object

9 9 Wrapper Classes & Constructors Wrapper classes BooleanCharacterDoubleFloat ByteShortIntegerLong Constructors public Integer( int value ) public Integer( String s ) public Double( double value ) public Double( String s )

10 10 Numeric Wrapper Class Methods public byte byteValue( ) public short shortValue( ) public int intValue( ) public long longValue( ) public float floatValue( ) public double doubleValue( ) Each method return the value in the data type implied by the method’s name

11 11 Wrapper Class Constants Wrapper classes are also a source of information about the primitive data types Constants available Integer.MIN_VALUE Integer.MAX_VALUE Float.MIN_VALUE Float.MAX_VALUE

12 12 Wrapper Classes public static void main(String[ ] args) throws IOException { int age; age = new Integer(keyboard.readLine( )).intValue( ); float balance; balance = new Float(keyboard.readLine( )).floatValue( ); double radius; radius = new Double(keyboard.readLine( )). doubleValue( ); }

13 13 Anonymous Class Definitions An anonymous object –A class instance that is not assigned to a reference variable new Integer(keyboard.readLine( )).intValue( ); new Circle( );

14 14 Our Output Approach For output we will use the following static PrintWriter screen = new PrintWriter(System.out, true); screen.print(“Hello World”); screen.flush( ); screen.println(name);

15 15 PrintWriter Advantages System.out is a print stream In previous implementations of Java the print streams have changed This subjects your programs to possible changes from one release to another PrintWriter class helps to insulate your program from print stream changes

16 16 FYI Wrapper classes are immutable –Information contained inside a wrapper class can not change


Download ppt "1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on."

Similar presentations


Ads by Google