1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done...

16
1 Java Console I/O Introduction

Transcript of 1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done...

Page 1: 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.

1

Java Console I/O

Introduction

Page 2: 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.

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

Page 3: 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.

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.

Page 4: 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.

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

Page 5: 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.

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

Page 6: 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.

6

Buffered Reader

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

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

7

Input Examplepublic class InputExample

{

static BufferedReader keyboard = new

BufferedReader(new InputStreamReader(System.in));

public static void main(String[ ] args)

throws IOException

{

String input;

input = keyboard.readLine( );

}

}

Page 8: 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.

8

Problems• If all we want to input are strings we are ok

• However what about other data types?

int float 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

Page 9: 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.

9

Wrapper Classes & Constructors• Wrapper classes

Boolean Character Double Float

Byte Short Integer Long

• Constructors

public Integer( int value )

public Integer( String s )

public Double( double value )

public Double( String s )

Page 10: 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.

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

Page 11: 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.

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

Page 12: 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.

12

Wrapper Classespublic 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( );

}

Page 13: 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.

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( );

Page 14: 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.

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);

Page 15: 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.

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

Page 16: 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.

16

FYI

• Wrapper classes are immutable– Information contained inside a wrapper class

can not change