Exception Handling
Exceptions: The runtime errors
• Exception handling enables a program to deal
with runtime errors and continue its normal
execution.
• Runtime errors occur while a program is running
if the JVM detects an operation that is
impossible to carry out.
Exceptions: The runtime errors
● When an exception is thrown, the method
that is executing must either deal with the
exception or throw it again.
● If the main method throws an exception,
the program halts and an error message is
displayed.
Exceptions
• In Java, runtime errors are thrown as
exceptions.
• An exception is an object that represents an
error or a condition that prevents execution
from proceeding normally.
• If the exception is not handled, the program
will terminate abnormally.
Exceptions Example
• If you access an array using an index that is
out of bounds, you will get a runtime error
with an
ArrayIndexOutOfBoundsException.
• If you enter a double value when your
program expects an integer, you will get a
runtime error with an
InputMismatchException.
Example
import java.util.Scanner;
public class Quotient {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter two integers: ");
int number1 = input.nextInt();
int number2 = input.nextInt();
System.out.println(number1 + " / " +
number2 + " is " + (number1 / number2));
}
One Solution
import java.util.Scanner;
public class QuotientWithIf {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter two integers: ");
int number1 = input.nextInt();
int number2 = input.nextInt();
if (number2 != 0)
System.out.println(number1 + " / " + number2 +
" is " + (number1 / number2));
else
System.out.println("Divisor cannot be zero ");
}
}
Handling Exceptions
• Exceptions are thrown from a method.
• The caller of the method can catch and
handle the exception.
• When an exception is thrown, the normal
execution flow is interrupted.
• As the name suggests, to “throw an
exception” is to pass the exception from one
place to another.
Handling Exceptions
• The statement for invoking the method is contained
in a try block and a catch block.
1. The try block contains the code that is executed in
normal circumstances.
2. The exception is caught by the catch block.
3. The code in the catch block is executed to handle
the exception.
4. Afterward, the statement after the catch block is
executed.
Example
public class QuotientWithException {
public static int quotient(int number1,
int number2)
{if (number2 == 0)
throw new ArithmeticException("Divisor
cannot be zero");
return number1 / number2; }
public static void main(String[ ] args)
{Scanner input = new Scanner(System.in);
System.out.print("Enter 2 integers: ");
int number1 = input.nextInt();
int number2 = input.nextInt();
try {
int result = quotient(number1, number2);
System.out.println(number1 + " / " + number2 + " is
“ + result); }
catch (ArithmeticException ex) {
System.out.println("Exception: an integer " +
"cannot be divided by zero ");
}
System.out.println("Execution continues ...");
}
}
A template for a
try-throw-catch block
• The throw statement is analogous to a method call, but
instead of calling a method, it calls a catch block.
• A catch block is like a method definition with a
parameter that matches the type of the value being
thrown.
try {
Code to run;
A statement or a method that may throw an
exception;
More code to run;
}
catch (type ex) {
Code to process the exception;
Advantages of Exception Handling
• It enables a method to throw an exception to
its caller, enabling the caller to handle the
exception.
• Without this capability, the called method
itself must handle the exception or terminate
the program.
Advantages of Exception Handling
• Often the called method does not know what
to do in case of error.
– This is typically the case for the library methods.
– The library method can detect the error, but only the
caller knows what needs to be done in case of error.
• Exception handling separates the detection
of an error (done in a called method) from the
handling of an error (done in the calling
method).
Many library methods throw exceptions
boolean continueInput = true;
do { try {
System.out.print("Enter an integer:
");
int number = input.nextInt();
System.out.println("Num entered" +
number);
continueInput = false; }
catch (InputMismatchException
ex) {
System.out.println("Try again.
Incorrect input");
Exception Types
• Exceptions are objects, and objects are
defined using classes.
• The root class for exceptions is
java.lang.Throwable
Exception Types
• The exception classes can be classified into
three major types: system errors, exceptions,
and runtime exceptions.
• System errors are thrown by the JVM and
are represented in the Error class.
– The Error class describes internal system errors,
though such errors rarely occur.
– Note: All of the errors occur at runtime
Exceptions thrown are
instances of the classes
Exceptions
Exception describes
errors caused by
Java program and
external
circumstances.
These errors can be
caught and handled
by our program.
Runtime Exceptions
RuntimeException is
caused by programming
errors, such as bad
casting, accessing an
out-of-bounds array, and
numeric errors.
Checked vs. Unchecked Exceptions
• A handler for an exception is found by
propagating the exception backward through
a chain of method calls, starting from the
current method.
• RuntimeException, Error and their
subclasses are known as unchecked
exceptions.
• All other exceptions are known as checked
exceptions, meaning that the compiler forces the
programmer to check and deal with the
exceptions.
Unchecked Exceptions
• Mostly, unchecked exceptions reflect programming
logic errors that are not recoverable.
• A NullPointerException is thrown if an object is
accessed through a reference variable before
an object is assigned to it
• An IndexOutOfBoundsException is thrown if we
access an element in an array outside the
bounds of the array.
• These are the logic errors that should be
corrected in the program.
• Unchecked exceptions can occur anywhere in
the program.
Unchecked Exceptions
Unchecked
exception.
Subclasses of Error
String s = "abc";
System.out.println(s.charAt(3));//StringIndexOutOfBound
sException
int[] list = new int[5];
System.out.println(list[5]); //
ArrayIndexOutOfBoundsException
Object o = null;
System.out.println(o.toString()); //
NullPointerException
Same Example
import java.util.Scanner;
public class DivideByZeroNoExceptionHandling {
public static int quotient(int numerator, int
denominator) {
return numerator / denominator; }
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter an integer
numerator: ");
int numerator = scanner.nextInt();
System.out.print("Please enter an integer
denominator: ");
int denominator = scanner.nextInt();
int result = quotient(numerator, denominator);
System.out.printf("%nResult: %d / %d = %d%n",
numerator, denominator, result); }
}
Declaring Exceptions
• Every method must state the types of checked
exceptions it might throw.
• This is known as declaring exceptions.
public void myMethod()
throws IOException
public void myMethod()
throws IOException, OtherException
Exception Handling Model:
Declaring, Throwing, and
Catching Exceptions
Throwing Exceptions
• When the program detects an error, the
program can create an instance of an
appropriate exception type and throw it.
• This is known as throwing an exception.
• throw new TheException();
• TheException ex = new TheException();
throw ex;
Throwing Exceptions Example
/** Set a new radius */
public void setRadius(double
newRadius)
throws IllegalArgumentException {
if (newRadius >= 0)
radius = newRadius;
else
throw new IllegalArgumentException(
"Radius cannot be negative");
}
Stack Trace
Stack Trace
• Several lines of information are displayed in
response to this invalid input.
• This information is known as a stack trace, which
includes the name of the exception
(java.lang.ArithmeticException) in a descriptive
message that indicates the problem that occurred
and the method-call stack (i.e., the call chain) at the
time it occurred.
• The stack trace includes the path of execution that
led to the exception method by method.
• The top row of the call chain indicates the throw point—the
initial point at which the exception occurred.
– This helps in debugging the program.
Stack Trace
• An InputMismatchException occurs when
Scanner method nextInt receives a string that
does not represent a valid integer.
• In place of the filename and line number, we may
be provided with the text Unknown Source.
• This means that the so-called debugging symbols
that provide the filename and line number
information for that method’s class were not
available to the JVM—this is typically the case for
the classes of the Java API.
– Many IDEs have access to the Java API source code
and will display filenames and line numbers in stack
traces.
Division By Zero
With Floating-point Values
• Java does allow this division
• Such a calculation results in the value positive or
negative infinity, which is represented in Java as a
floating point value (but displays as the string Infinity
or -Infinity).
• If 0.0 is divided by 0.0, the result is NaN (not a
number), which is also represented in Java as a
floating-point value (but displays as NaN).
• If you need to compare a floating-point value to NaN,
use the method isNaN of class Float (for float values)
or of class Double (for double values).
Catching Exceptions
try {
statements;
… // Statements that may throw exceptions
}
catch (Exception1 exVar1) {
handler for exception1;
}
catch (Exception2 exVar2) {
handler for exception2;
}
...
catch (ExceptionN exVar3) {
handler for exceptionN;
} 36
• If no exceptions arise during the execution
of the try block, the catch blocks are
skipped.
Example
public class TestException3 {
public static int quotient(int numerator, int denominator)throws ArithmeticException
{
return numerator / denominator;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean continueLoop = true;
do {
}
while (continueLoop);
Example contd.
try { // read two numbers and calculate quotient
System.out.print("Please enter an integer numerator: ");
int numerator = scanner.nextInt();
System.out.print("Please enter an integer denominator: ");
int denominator = scanner.nextInt();
int result = quotient(numerator, denominator);
System.out.printf("%nResult: %d / %d = %d%n", numerator,
denominator, result);
continueLoop = false; // input successful; end looping
catch (InputMismatchException inputMismatchException) {
System.err.printf("%nException: %s%n",
inputMismatchException);
scanner.nextLine(); // discard input so user can try again
System.out.printf("You must enter integers. Try again.%n%n");
catch (ArithmeticException arithmeticException) {
Exception Handler
• The code that handles the exception is called the exception
handler; it is found by propagating the exception backward
through a chain of method calls, starting from the current
method.
• Each catch block is examined in turn, from first to last,
to see whether the type of the exception object is an
instance of the exception class in the catch block.
– If so, the exception object is assigned to the variable declared and
the code in the catch block is executed.
– If no handler is found, Java exits this method, passes the exception
to the method’s caller, and continues the same process to find a
handler.
– If no handler is found in the chain of methods being invoked, the
program terminates and prints an error message on the console.
• The process of finding a handler is called catching an
exception.
Catching Exceptions
Exception Scenario for the Example
• If the exception type is Exception3, it is caught by the catch block
for handling exception ex3 in method2. statement5 is skipped, and
statement6 is executed.
• If the exception type is Exception2, method2 is aborted, the control
is returned to method1, and the exception is caught by the catch
block for handling exception ex2 in method1. statement3 is
skipped, and statement4 is executed.
• If the exception type is Exception1, method1 is aborted, the control
is returned to the main method, and the exception is caught by the
catch block for handling exception ex1 in the main method.
statement1 is skipped, and statement2 is executed.
• If the exception type is not caught in method2, method1, or main,
the program terminates, and statement1 and statement2 are not
executed.
Important Note
• Various exception classes can be derived from
a common superclass.
• If a catch block catches exception objects of a
superclass, it can catch all the exception
objects of the subclasses of that superclass.
• The order in which exceptions are specified in
catch blocks is important.
• A compile error will result if a catch block for a
superclass type appears before a catch block
for a subclass type.
What is displayed when the following
program is run (1)?
public class Test {
public static void main(String[] args) {
try {
method();
System.out.println("After the method call");
}
catch (RuntimeException ex) {
System.out.println("RuntimeException in main");
}
catch (Exception ex) {
System.out.println("Exception in main");
}
}
static void method() throws Exception {
try {
String s = "abc";
System.out.println(s.charAt(3));
}
catch (RuntimeException ex) {
System.err.println("RuntimeException in method()“ + ex);
}
After the method call
RuntimeException in method()
java.lang.StringIndexOutOfBoundsException:
String index out of range: 3
What is displayed when the following
program is run (2)?
public class TestException5 {
public static void main(String[] args) {
try {
int[] list = new int[10];
System.out.println("list[10] is " + list[10]);
}
catch (ArithmeticException ex) {
System.out.println("ArithmeticException" + ex);
}
catch (RuntimeException ex) {
System.out.println("RuntimeException" + ex);
}
catch (Exception ex) {
RuntimeExceptionjava.lang.ArrayIndexOutOfBoundsException: 10
System.out.println("Exception" + ex);
}}}
public class TestException6 {
public static void main(String[] args) {
try {
method();
System.out.println("After the method call");
}
catch (ArithmeticException ex) {
System.out.println("ArithmeticException" + ex);}
catch (RuntimeException ex) {
System.out.println("RuntimeException" + ex);}
catch (Exception ex) {
System.out.println("Exception" + ex);
}}
static void method() throws Exception {
System.out.println(1 / 0);}
}
ArithmeticExceptionjava.lang.ArithmeticException: / by zero
Java forces you to deal with checked
exceptions
• If a method declares a checked exception (i.e., an exception
other than Error or RuntimeException), we must invoke it in a
try-catch block or declare to throw the exception in the calling
method.
• For example, suppose that method p1 invokes method p2,
and p2 may throw a checked exception (e.g., IOException)
The finally Clause
try {
statements;
}
catch(TheException ex) {
handling ex;
}
finally {
finalStatements;
}
Rethrowing Exceptions
• Java allows an exception handler to rethrow the
exception if the handler cannot process the
exception, or simply wants to let its caller be
notified of the exception.
• try {
statements;
}
catch(TheException ex) {
perform operations before exits;
throw ex;
}
The finally Clause
• The code in the finally block is executed under all circumstances,
regardless of whether an exception occurs in the try block or is caught.
• Consider three possible cases:
1. If no exception arises in the try block, finalStatements is executed
and the next statement after the try statement is executed.
2. If a statement causes an exception in the try block that is caught
in a catch block, the rest of the statements in the try block are
skipped, the catch block is executed, and the finally clause is
executed. The next statement after the try statement is executed.
3. If one of the statements causes an exception that is not caught in
any catch block, the other statements in the try block are skipped,
the finally clause is executed, and the exception is passed to the
caller of this method.
• The finally block executes even if there is a return statement prior to
reaching the finally block.
Trace a Program Execution
Suppose no exceptions
in the statements
try {
statements;
}
catch(TheException ex)
{
handling ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
The final block is
always executed
try {
statements;
}
catch(TheException ex)
{
handling ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
Next statement in the
method is executed
try {
statements;
}
catch(TheException ex)
{
handling ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
try { Suppose an exception of
type Exception1 is thrown
statement1; in statement2
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
try { The exception is handled.
statement1;
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
try { The final block is always
executed.
statement1;
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
try { The next statement in the
method is now executed.
statement1;
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
try {
statement1; statement2 throws an
statement2; exception of type
statement3; Exception2.
}
catch(Exception1 ex) {
handling ex;
}
catch(Exception2 ex) {
handling ex;
throw ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
try {
statement1; Handling exception
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
catch(Exception2 ex) {
handling ex;
throw ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
try {
statement1; Execute the final block
statement2;
statement3;
}
catch(Exception1 ex) {
handling ex;
}
catch(Exception2 ex) {
handling ex;
throw ex;
}
finally {
finalStatements;
}
Next statement;
Trace a Program Execution
try {
statement1; Rethrow the exception and
statement2; control is transferred to the
statement3; caller
}
catch(Exception1 ex) {
handling ex;
}
catch(Exception2 ex) {
handling ex;
throw ex;
}
finally {
finalStatements;
}
Next statement;
Problem
try {
statement1;
statement2;
statement3;
}
catch (Exception1 ex1) {
}
finally {
statement4;
}
statement5;
• Answer the following questions:
a. If no exception occurs, will statement4 or statement5 be executed?
b. If the exception is of type Exception1, will statement4 or statement5
be executed?
Problem
• Suppose that statement2 may cause an exception in the following code:
try {
statement1;
statement2;
statement3;
}
catch (Exception1 ex1) {
}
catch (Exception2 ex2) {
throw ex2;
}
finally {
statement4;
}
statement5;
Answer the following questions:
What is the output of the Code?
public class TestFinally {
public static void main(String[] args) {
try {
System.out.println("Welcome to Java");
int i = 0;
int y = 2 / i;
System.out.println("Welcome to HTML");
}
finally {
System.out.println("The finally clause is executed");
}
}
}
What is the output of the Code?
public class TestFinallyUsingExceptions {
public static void main(String[] args) {
try {
throwException();
}
catch (Exception exception) { // exception thrown
System.err.println("Exception handled in main " + exception);
}
doesNotThrowException();
}
// demonstrate try...catch...finally
public static void throwException() throws Exception {
try { // throw an exception and catch it
System.out.println("Method throwException");
throw new Exception(); // generate exception
}
catch (Exception exception) { // catch
System.err.println("Exception handled in method throwException");
System.out.println("Back in Catch Block");
// throw exception; // rethrow for processing
// code here would not be reached; would cause compilation errors
}
finally { // executes regardless of what occurs //in try...catch
System.err.println("Finally executed in throwException");
}
// code here would not be reached; would cause compilation errors
Output
Method throwException
Exception handled in method throwException
After Catch of main
Finally executed in throwException
Method doesNotThrowException
End of method doesNotThrowException
Exception handled in main java.lang.Exception
Finally executed in doesNotThrowException
When to Use Exceptions
• A method should throw an exception if the error needs to be handled by its caller.
• Simple errors in individual methods are best handled without throwing exceptions.
• This can be done by using if statements to check for errors.
• Use a try-catch block when dealing with unexpected error conditions only.
• For example, the following code:
try {
System.out.println(refVar.toString());
catch (NullPointerException ex) {
System.out.println("refVar is null");
}
is better replaced by
if (refVar != null)
System.out.println(refVar.toString());
Chained Exceptions
• Throwing a new exception (with additional information) along with the original
exception.
public class TestChainedException {
public static void main(String[] args) {
try {
method1();}
catch (Exception ex){System.out.println("Here in caller");
ex.printStackTrace();
}
}
public static void method1() throws Exception {
try {
method2(); }
catch (Exception ex) {
throw new Exception("New info from method1", ex);
}
}
public static void method2() throws Exception {
Output
Here in caller
java.lang.Exception: New info from method1
at
mayankdave.TestChainedException.method1(TestChainedExcep
tion.java:28)
at
mayankdave.TestChainedException.main(TestChainedExceptio
n.java:15)
Caused by: java.lang.Exception: New info from
method2
at
mayankdave.TestChainedException.method2(TestChainedE
xception.java:32)
at
mayankdave.TestChainedException.method1(TestChainedE
Example
class TestCustomException{
public static void main(String args[]){
try{
validate(13); }
catch(Exception e){
System.out.println("Exception occured: "+ e);
}
System.out.println("rest of the code..."); }
static void validate(int age)throws InvalidAgeException{
if(age<18)
throw new InvalidAgeException("Age not valid");
else
System.out.println("welcome to vote");
}
}
class InvalidAgeException extends Exception {
InvalidAgeException(String s){ super(s); }
}
The File Class
• File class contains the methods for obtaining
the properties of a file/directory, and for
renaming and deleting a file/directory.
• File class is intended to provide an
abstraction that deals with most of the
machine-dependent complexities of files and
path names in a machine-independent
fashion.
• The filename is a string.
• The File class is a wrapper class for the file
name and its directory path.
The File Class
• The filename is a string.
• Absolute Path Name
• Relative Path Name
• The File class is a wrapper class for the file
name and its directory path.
• However, the File class does not contain the
methods for reading and writing file contents.
•\
•/
• \\
Obtaining file properties and
manipulating file
Example
public class TestFileClass {
public static void main(String[] args) {
java.io.File file = new
java.io.File("image/dsc_0133.jpg");
System.out.println("Does it exist? " + file.exists());
System.out.println("The file has " + file.length() + "
bytes");
System.out.println("Can it be read? " + file.canRead());
System.out.println("Can it be written? " +
file.canWrite());
System.out.println("Is it a directory? " +
file.isDirectory());
System.out.println("Is it a file? " + file.isFile());
System.out.println("Is it absolute? " +
file.isAbsolute());
System.out.println("Is it hidden? " + file.isHidden());
System.out.println("Absolute path is " +
Write Data
public class TestFileWriteData {
public static void main(String[] args) throws
IOException {
java.io.File file = new java.io.File("scores.txt");
if (file.exists()) {
System.out.println("File already exists");
System.exit(1);
}
// Create a file
java.io.PrintWriter output = new
java.io.PrintWriter(file);
// Write formatted output to the file
output.print("John T Smith ");
output.println(90);
output.print("Eric K Jones ");
output.println(85);
output.close();
Text I/O
• A File object encapsulates the properties of a file
or a path, but does not contain the methods for
reading/writing data from/to a file.
• In order to perform I/O, you need to create objects
using appropriate Java I/O classes.
• The objects contain the methods for
reading/writing data from/to a file.
• You may use FileWriter, FileReader
• Or use Scanner and PrintWriter classes.
Major Differences
• FileWriter throws IOException in case of any IO
failure.
• None of the PrintWriter methods throws
IOException, instead they set a boolean flag which
can be obtained using checkError().
• PrintWriter comes with an option of autoflush while
creation(default is without autoflush) which will flush
after every byte of data is written.
• In case of FileWriter, caller has to take care of
invoking flush (no buffering).
Writing Data Using PrintWriter
Reading Data Using Scanner
import java.io.*;
public class MainFile{
public static void main(String[] args) throws Exception
{
PrintWriter pw = new PrintWriter(new File("data"));
for(int i=1;i<=10;++i)
pw.print(i);
pw.close();
}
}
Try-with-resources
• Programmers often forget to close the file.
• JDK 7 provides the followings new
try-with-resources syntax that automatically closes
the files.
try (declare and create resources) {
Use the resource to process the file;
}
Using try for auto close
public class WriteDataWithAutoClose {
public static void main(String[] args) throws
Exception {
java.io.File file = new java.io.File("scores.txt");
if (file.exists()) {
System.out.println("File already exists");
System.exit(0);
}
try ( // Create a file
java.io.PrintWriter output = new java.io.PrintWriter(file);)
{
output.print("John T Smith ");
output.println(90);
output.print("Eric K Jones ");
output.println(85);
} }
Read Data
import java.util.Scanner;
public class ReadData {
public static void main(String[] args) throws Exception {
// Create a File instance
java.io.File file = new java.io.File("scores.txt");
// Create a Scanner for the file
Scanner input = new Scanner(file);
while (input.hasNext()) {
String firstName = input.next();
String mi = input.next();
String lastName = input.next();
int score = input.nextInt();
System.out.println(
firstName + " " + mi + " " + lastName + " " + score);
}
// Close the file
input.close();
}
}
Case
• Suppose a text file named test.txt contains a line 34 567
• After the following code is executed,
Scanner input = new Scanner(new File("test.txt"));
int intValue = input.nextInt();
String line = input.nextLine();
• intValue contains 34 and line contains the characters ' ', 5, 6, and 7.
• What happens if the input is entered from the keyboard?
• Suppose you enter 34, press the Enter key, then enter 567 and press the Enter key
for the following code:
Scanner input = new Scanner(System.in);
int intValue = input.nextInt();
String line = input.nextLine();
• You will get 34 in intValue and an empty string in line.
• The token-based input method nextInt()reads in 34 and stops at the delimiter,
which in this case is a line separator (the Enter key).
• The nextLine()method ends after reading the line separator and returns the string
read before the line separator.
• Since there are no characters before the line separator, line is empty.
Scanner input = new Scanner("13 14");
int sum = input.nextInt() +
input.nextInt();
System.out.println("Sum is " + sum);
import java.io.RandomAccessFile;
public class TestRandomAccessFile {
public static void main(String[] args) throws IOException {
try ( // Create a random access file
RandomAccessFile inout = new RandomAccessFile("inout.dat", "rw");
){
// Clear the file to destroy the old contents if exists
inout.setLength(0);
// Write new integers to the file
for (int i = 0; i < 200; i++)
inout.writeInt(i);
// Display the current length of the file
System.out.println("Current file length is " + inout.length());
// Retrieve the first number
inout.seek(0); // Move the file pointer to the beginning
System.out.println("The first number is " + inout.readInt());
// Retrieve the second number
inout.seek(1 * 4); // Move the file pointer to the second number
System.out.println("The second number is " + inout.readInt());