Java Programming Transparency No. 1 Lecture 6. Java I/O Cheng-Chia Chen.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done...
-
Upload
darcy-anthony -
Category
Documents
-
view
215 -
download
1
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...
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 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
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
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
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
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
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( );
}
}
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
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 )
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
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
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( );
}
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
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
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
FYI
• Wrapper classes are immutable– Information contained inside a wrapper class
can not change