Download 4. Reading Input

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Contents
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
4-1
Introduction
Types and Variables
Statements and Control Flow
Reading Input
Classes and Objects
Arrays
Methods
Scope and Lifetime
Utility classes
Introduction to Object-Oriented Analysis
and Design
Copyright (c) 1999-2004 N. Afshartous
Chapter 4: Reading Input
After this chapter you will be able to:
– catch exceptions
– read user input
4-2
Copyright (c) 1999-2004 N. Afshartous
readLine()
• Calling readLine() reads user input up to
the carriage return
• readLine must be called via an object of
type BufferedReader
• Two steps
– create an object of type BufferedReader
– call readLine
4-3
Copyright (c) 1999-2004 N. Afshartous
First Step
• System.in is associated with the keyboard
input stream
• Use System.in to construct an object of type
BufferedReader
BufferedReader input = new BufferedReader(
new InputStreamReader(
System.in));
• Objects are constructed by invoking new
4-4
Copyright (c) 1999-2004 N. Afshartous
First Step Continued
• Names BufferedReader and
InputStreamReader must be imported
import java.io.BufferedReader;
import java.io.InputStreamReader;
BufferedReader input = new BufferedReader(
new InputStreamReader(
System.in));
• Use import to refer to names defined in
external packages
4-5
Copyright (c) 1999-2004 N. Afshartous
Second Step
• Call readLine to get input line
import java.io.BufferedReader;
import java.io.InputStreamReader;
BufferedReader input = new BufferedReader(
new InputStreamReader(
System.in));
String inputLine = input.readLine();
• readLine returns an object of type String
4-6
Copyright (c) 1999-2004 N. Afshartous
Exceptions
• Exceptions correspond to run-time errors
• There are many kinds of Java Exceptions
–
–
–
–
–
4-7
IOException
EOFException
FileNotFoundException
UnknownHostException
MalformedURLException
and many more
Copyright (c) 1999-2004 N. Afshartous
Throwing Exceptions
• A method can declare that it may throw an
exception
public String readLine() throws IOException
public static int parseInt(String
throws NumberFormatException
4-8
s)
Copyright (c) 1999-2004 N. Afshartous
Catching Exceptions
• An exception may be caught and handled using
try and catch
BufferedReader input = new BufferedReader(
new InputStreamReader(
System.in));
try {
String inputLine = input.readLine();
} catch (java.io.IOException e) {
System.out.println("oops! An input error");
}
• If no exception occurs then the catch branch
is not executed
4-9
Copyright (c) 1999-2004 N. Afshartous
Compile-time Checking
• The compiler will force you to handle certain
exceptions
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Read {
public static void main (String [] args) {
BufferedReader input = new BufferedReader(
new InputStreamReader(
System.in));
String inputLine = input.readLine();
}
}
• What is wrong ?
4-10
Copyright (c) 1999-2004 N. Afshartous
Compile-time Checking
• Answer: The exception must be caught
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Read {
public static void main (String [] args) {
BufferedReader input = new BufferedReader(
new InputStreamReader(
System.in));
try {
String inputLine = input.readLine();
} catch (java.io.IOException e) {
System.out.println("oops! An input error");
}
}
}
4-11
Copyright (c) 1999-2004 N. Afshartous
An Echo Program
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Echo {
public static void main (String [] args) {
BufferedReader input = new BufferedReader(
new InputStreamReader(System.in));
while (true) {
System.out.println("type in a line to echo");
try {
String result = input.readLine();
System.out.println("You typed: " + result);
} catch (java.io.IOException e) {
System.out.println("oops! An input error");}
}
}
}
4-12
Copyright (c) 1999-2004 N. Afshartous
Execution
• User’s lines are echoed
Input/Output
Input/Output
4-13
type in a line to echo
ok, how about this ?
ok, how about this ?
type in a line to echo
here is another
here is another
Copyright (c) 1999-2004 N. Afshartous
It’s Exercise Time
4-14
Copyright (c) 1999-2004 N. Afshartous