introduction to java - clark science center · introduction to java csc212 lecture 7 d. thiebaut,...
TRANSCRIPT
Introduction to JavaCSC212 Lecture 7
D. Thiebaut, Fall 2014
More on Exceptions
A Simple Classimport 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 #1while ( 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 #2import 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 #2import 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…!! ! ! ! ! ! continue;! }! ! //... some code missing...
Both Input!
Statements!Protected!
But There's a Problem… !
Infinite Loop… !
It Has to Do With The 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() —->
Input Buffer
Solution: input.nextLine();
1 2 A _ 3 7 \n Input Buffer
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…!! ! ! ! ! ! input.nextLine();!! ! ! ! ! ! continue;! }! ! //... some code missing...
Get rid!of the rest of!the unread!information!
Fix #2 Fixed
Reading Files… (and dealing with more exceptions)
import java.io.File;!import java.util.Scanner;!!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 );! }! }!}
Example
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!
–en.wikipedia.org/wiki/Data_structureWikipedia
“ 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.”
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!
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 }
Access Element At Index
for ( int i=0; i<list.size(); i++ ) { int x = list.get( i ); System.out.println( x ); }
Iterating Through Vector
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 ); } }}