Lecture 08: IntroductionToConsoleIO

Download Report

Transcript Lecture 08: IntroductionToConsoleIO

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
1
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 type
Wrapper class
char
Character
double
Double
float
Float
int
Integer
long
Long
 Wrapper classes are used to provide constants and general methods for the primitive
data
types.
2
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 class
parse method
Double
parseDouble(string)
Float
parseFloat(string)
Integer
parseInt(string)
Long
parseLong(string)
 Examples:
int numStudents = Integer.parseInt(“500”);
String inputLine = “3.5”;
double studentGPA = Double.parseDouble(inputLine);
3
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].
4
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.
5
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);
}
}
6
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));
}
}
7