Download import java.util.InputMismatchException

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
Introduction to Java
CSC212 Lecture 7
D. Thiebaut, Fall 2014
More on Exceptions
A Simple Class
import java.util.Scanner;!
!
public class Bomb {!
!
public static void main(String[] args) {!
Scanner input = new Scanner( System.in );!
!
while ( true ) {!
System.out.println( "Please enter 2 integers"!
+" (-1 to stop): ");!
int n1 = input.nextInt();!
if ( n1==-1 ) !
break;!
int n2 = input.nextInt();!
System.out.println( n1 + " / " + n2 + " is " + (n1/n2) );!
}!
}!
}
What could go wrong?
import java.util.Scanner;!
!
public class Bomb {!
!
public static void main(String[] args) {!
Scanner input = new Scanner( System.in );!
!
while ( true ) {!
System.out.println( "Please enter 2 integers"!
+" (-1 to stop): ");!
int n1 = input.nextInt();!
if ( n1==-1 ) !
break;!
int n2 = input.nextInt();!
System.out.println( n1 + " / " + n2 + " is " + (n1/n2) );!
}!
}!
}
First Place to Crash:
import java.util.Scanner;!
!
public class Bomb {!
!
public static void main(String[] args) {!
Scanner input = new Scanner( System.in );!
!
while ( true ) {!
System.out.println( "Please enter 2 integers"!
+" (-1 to stop): ");!
int n1 = input.nextInt();!
if ( n1==-1 ) !
break;!
int n2 = input.nextInt();!
System.out.println( n1 + " / " + n2 + " is " + (n1/n2) );!
}!
}!
}
Fix #1
while ( true ) {!
System.out.println( "Please enter 2 integers (-1 to stop): ");!
int n1 = input.nextInt();!
int n2 = input.nextInt();!
if ( n1==-1 ) !
break;!
try {!
System.out.println( n1 + " / " + n2 + " is " + (n1/n2) );!
}!
catch ( ArithmeticException e ) {!
System.out.println("Exception: " + e.getMessage() + !
"\nReenter numbers." );!
}!
}!
What Else Could Go Wrong?
import java.util.Scanner;!
!
public class Bomb {!
!
public static void main(String[] args) {!
Scanner input = new Scanner( System.in );!
!
while ( true ) {!
System.out.println( "Please enter 2 integers"!
+" (-1 to stop): ");!
int n1 = input.nextInt();!
if ( n1==-1 ) !
break;!
int n2 = input.nextInt();!
System.out.println( n1 + " / " + n2 + " is " + (n1/n2) );!
}!
}!
}
Badly Formatted Ints:
import java.util.Scanner;!
!
public class Bomb {!
!
public static void main(String[] args) {!
Scanner input = new Scanner( System.in );!
!
while ( true ) {!
System.out.println( "Please enter 2 integers"!
+" (-1 to stop): ");!
int n1 = input.nextInt();!
if ( n1==-1 ) !
break;!
int n2 = input.nextInt();!
System.out.println( n1 + " / " + n2 + " is " + (n1/n2) );!
}!
}!
}
Fix #2
import java.util.Scanner;!
import java.util.InputMismatchException;!
!
public class Bomb {!
!
public static void main(String[] args) {!
Scanner input = new Scanner( System.in );!
!
while ( true ) {!
System.out.println( "Please enter 2 integers (-1 to stop): ");!
int n1, n2;!
try {!
n1 = input.nextInt();!
if ( n1==-1 ) break;!
n2 = input.nextInt();!
} catch ( InputMismatchException e ) {!
// add your code here...!
}!
!
//... some code missing...
Fix #2
import java.util.Scanner;!
import java.util.InputMismatchException;!
!
public class Bomb {!
!
public static void main(String[] args) {!
Scanner input = new Scanner( System.in );!
!
while ( true ) {!
Both Input!
System.out.println( "Please enter 2 integers (-1 to stop): ");!
Statements!
int n1, n2;!
Protected!
try {!
n1 = input.nextInt();!
if ( n1==-1 ) break;!
n2 = input.nextInt();!
} catch ( InputMismatchException e ) {!
// add your code here…!
! ! ! ! ! !
continue;!
}!
!
//... some code missing...
But There's a Problem…
!
Infinite Loop…
!
It Has to Do With The
Input Buffer…
Input Buffer
1 2 3 _ 3 7 \n
input.nextInt() —-> 123
input.nextInt() —-> 37
Input Buffer
1 2 A _ 3 7 \n
input.nextInt() —->
input.nextInt() —->
Solution: input.nextLine();
Input Buffer
1 2 A _ 3 7 \n
Fix #2 Fixed
import java.util.Scanner;!
import java.util.InputMismatchException;!
!
public class Bomb {!
!
public static void main(String[] args) {!
Scanner input = new Scanner( System.in );!
!
while ( true ) {!
System.out.println( "Please enter 2 integers (-1 to stop): ");!
int n1, n2;!
try {!
n1 = input.nextInt();!
if ( n1==-1 ) break;!
Get rid!
n2 = input.nextInt();!
of the rest of!
} catch ( InputMismatchException e ) {!
the unread!
// add your code here…!
information!
! ! ! ! ! !
input.nextLine();!
! ! ! ! ! !
continue;!
}!
!
//... some code missing...
Reading Files…
(and dealing with
more exceptions)
import java.io.File;!
import java.util.Scanner;!
!
Example
public class Lab5ReadFile {!
public static void main( String[] args ) {!
!
String fileName;!
int n1, n2;!
Scanner inFile = null;!
!
// if user forgot the enter file name on command line, remind her!
if ( args.length == 0 ) {!
System.out.println( "Syntax: java progName textFileName" );!
System.out.println( "
where textFileName is a name of a data file.");!
return;!
}!
!
// set file name to first argument on command line!
fileName = args[0];
!
inFile = new Scanner( new File( fileName ) );!
!
// read the file!
while ( inFile.hasNext() ) {!
n1 = inFile.nextInt();!
n2 = inFile.nextInt();!
System.out.println( n1 + " / " + n2 + " is " + n1/n2 );!
}!
}!
}
When we compile the
program, the compiler stops
and requires the use of a
try/catch statement!
$: javac Lab5ReadFile.java
Lab5ReadFile.java:23: error: unreported exception FileNotFoundException; must be caught or
declared to be thrown
inFile = new Scanner( new File( fileName ) );
^
1 error
Search the Error on the Web
import java.io.File;!
import java.util.Scanner;!
import java.io.FileNotFoundException;!
!
public class Lab5ReadFile {!
public static void main( String[] args ) {!
!
String fileName;!
int n1, n2;!
Scanner inFile = null;!
…!
!
// set file name to first argument on command line!
fileName = args[0]; !
!
try {
!
inFile = new Scanner( new File( fileName ) );!
}!
catch ( FileNotFoundException e ) {!
System.out.println( "File not found." );!
return;!
}!
!
…!
}!
}
(Lab5ReadFile.java)
Throwing Exceptions
Why?
•
Sometimes the method in which the exception
occurs is too low level to be able to fix the
problem.
private static int[] get2Ints( Scanner inFile ) {!
int n1 = inFile.nextInt(); !
int n2 = inFile.nextInt();!
return new int[] { n1, n2 };!
}
private static int[] get2Ints( Scanner inFile ) !
! ! ! ! ! ! ! ! ! ! throws InputMismatchException{!
int n1 = inFile.nextInt(); !
int n2 = inFile.nextInt();!
return new int[] { n1, n2 };!
}
while (inFile.hasNext()) {!
int[] a;!
try {!
a = get2Ints( inFile );!
} catch ( InputMismatchException e ) {!
System.err.println( "Wrong number format. Please reenter!" );!
inFile.nextLine();!
continue;
}!
System.out.println( a[0] + " / " + a[1] + " is " + a[0] / a[1] );!
}
private static int[] get2Ints( Scanner inFile ) !
! ! ! ! ! ! ! ! ! ! throws InputMismatchException{!
int n1 = inFile.nextInt(); !
int n2 = inFile.nextInt();!
return new int[] { n1, n2 };!
}
while (inFile.hasNext()) {!
int[] a;!
try {!
a = get2Ints( inFile );!
} catch ( InputMismatchException e ) {!
System.err.println( "Wrong number format. Please reenter!" );!
inFile.nextLine();!
continue;
}!
System.out.println( a[0] + " / " + a[1] + " is " + a[0] / a[1] );!
}
We Create Our
Own Exception Object
class MyException extends Exception {!
MyException( String message ) {!
super( message );!
}!
}
private static int[] get2Ints( Scanner inFile ) !
! ! !
throws InputMismatchException, MyException {!
int n1 = inFile.nextInt(); !
int n2 = inFile.nextInt();!
if ( n2 == 0 ) {!
throw new MyException( "Invalid 0\n" );!
}!
return new int[] { n1, n2 };!
}!
(Lab5ReadFileThrow3.java)
DATA
STRUCTURES!
“ In computer science, a data structure
is a particular way of organizing data in
a computer so that it can be used
efficiently. Different kinds of data
structures are suited to different kinds of
applications, and some are highly
specialized to specific tasks.”
–en.wikipedia.org/wiki/Data_structureWikipedia
Efficiency
•
SPEED!
•
MEMORY
Java’s Vector
Data Structure
(Chapter 3)
Where Do I Find Information?
http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html
Some Useful Vector
Methods
•
Vector(), Vector(initialCapacity)
•
add( )
•
contains( Object )
•
get( index )
•
indexOf( Object )
•
size()
Build a Vector…
import java.util.Iterator;
import java.util.Vector;
!
!
public class Vector1 {
!
}
static public void main( String[] args ) {
Vector list = new Vector( );
for ( int i=0; i<10; i++ ) list.add( (Integer) (i * 3) );
… }
Build a Vector…
import java.util.Iterator;
import java.util.Vector;
!
!
public class Vector1 {
!
}
static public void main( String[] args ) {
Vector list = new Vector( );
for ( int i=0; i<10; i++ ) list.add( (Integer) (i * 3) );
… }
Type Cast!
Access Element At Index
try {
System.out.println( "Element at 4 = " + list.get( 4 ) );
} catch (ArrayIndexOutOfBoundsException e ) {
// do nothing
}
!
try {
System.out.println( "Element at 20 = " + list.get( 20 ) );
} catch (ArrayIndexOutOfBoundsException e ) {
// do nothing
}
Iterating Through Vector
for ( int i=0; i<list.size(); i++ ) {
int x = list.get( i );
System.out.println( x );
}
Iterator<Integer> it = list.iterator();
while ( it.hasNext() ) {
int x = it.next();
System.out.println( x );
}
import java.util.Iterator;
import java.util.Vector;
!
public class Vector1 {
!
static public void main( String[] args ) {
Vector list = new Vector( );
for ( int i=0; i<10; i++ ) list.add( (Integer) (i * 3) );
try {
System.out.println( "Element at 4 = " + list.get( 4 ) );
} catch (ArrayIndexOutOfBoundsException e ) {
// do nothing
}
!
}
}
try {
System.out.println( "Element at 20 = " + list.get( 20 ) );
} catch (ArrayIndexOutOfBoundsException e ) {
// do nothing
}
Iterator<Integer> it = list.iterator();
while ( it.hasNext() ) {
int x = it.next();
System.out.println( x );
} import java.util.Iterator;
import java.util.Vector;
!
public class Vector2 {
static public void main( String[] args ) {
Vector<Integer> list = new Vector<Integer>( );
for ( int i=0; i<10; i++ ) list.add( i * 3 );
try {
System.out.println( "Element at 4 = " + list.get( 4 ) );
} catch (ArrayIndexOutOfBoundsException e ) {
// do nothing
}
try {
System.out.println( "Element at 20 = " + list.get( 20 ) );
} catch (ArrayIndexOutOfBoundsException e ) {
// do nothing
}
Iterator<Integer> it = list.iterator();
while ( it.hasNext() ) {
int x = it.next();
System.out.println( x );
}
}
}
Related documents