![Page 1: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/1.jpg)
Introduction to JavaCSC212 Lecture 7
D. Thiebaut, Fall 2014
![Page 2: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/2.jpg)
More on Exceptions
![Page 3: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/3.jpg)
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) );! }! }!}
![Page 4: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/4.jpg)
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) );! }! }!}
![Page 5: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/5.jpg)
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) );! }! }!}
![Page 6: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/6.jpg)
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." );! }!}!
![Page 7: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/7.jpg)
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) );! }! }!}
![Page 8: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/8.jpg)
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) );! }! }!}
![Page 9: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/9.jpg)
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...
![Page 10: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/10.jpg)
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!
![Page 11: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/11.jpg)
But There's a Problem… !
Infinite Loop… !
It Has to Do With The Input Buffer…
![Page 12: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/12.jpg)
1 2 3 _ 3 7 \n
input.nextInt() —-> 123
input.nextInt() —-> 37
Input Buffer
![Page 13: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/13.jpg)
1 2 A _ 3 7 \n
input.nextInt() —->
input.nextInt() —->
Input Buffer
![Page 14: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/14.jpg)
Solution: input.nextLine();
1 2 A _ 3 7 \n Input Buffer
![Page 15: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/15.jpg)
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
![Page 16: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/16.jpg)
Reading Files… (and dealing with more exceptions)
![Page 17: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/17.jpg)
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
![Page 18: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/18.jpg)
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
![Page 19: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/19.jpg)
Search the Error on the Web
![Page 20: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/20.jpg)
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;! }! ! …! }!}
![Page 21: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/21.jpg)
(Lab5ReadFile.java)
![Page 22: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/22.jpg)
Throwing Exceptions
![Page 23: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/23.jpg)
Why?
• Sometimes the method in which the exception occurs is too low level to be able to fix the problem.
![Page 24: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/24.jpg)
private static int[] get2Ints( Scanner inFile ) {! int n1 = inFile.nextInt(); ! int n2 = inFile.nextInt();! return new int[] { n1, n2 };! }
![Page 25: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/25.jpg)
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] );! }
![Page 26: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/26.jpg)
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] );! }
![Page 27: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/27.jpg)
We Create Our Own Exception Object
![Page 28: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/28.jpg)
class MyException extends Exception {! MyException( String message ) {! super( message );! }!}
![Page 29: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/29.jpg)
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 };!}!
![Page 30: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/30.jpg)
(Lab5ReadFileThrow3.java)
![Page 31: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/31.jpg)
DATA STRUCTURES!
![Page 32: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/32.jpg)
–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.”
![Page 33: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/33.jpg)
Efficiency
• SPEED!
• MEMORY
![Page 34: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/34.jpg)
Java’s Vector Data Structure
(Chapter 3)
![Page 35: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/35.jpg)
Where Do I Find Information?http://docs.oracle.com/javase/7/docs/api/java/util/Vector.html
![Page 36: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/36.jpg)
Some Useful Vector Methods
• Vector(), Vector(initialCapacity)
• add( )
• contains( Object )
• get( index )
• indexOf( Object )
• size()
![Page 37: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/37.jpg)
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) ); … }}
![Page 38: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/38.jpg)
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!
![Page 39: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/39.jpg)
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
![Page 40: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/40.jpg)
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 ); }
![Page 41: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/41.jpg)
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 ); } }}
![Page 42: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/42.jpg)
![Page 43: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/43.jpg)
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 ); } }}
![Page 44: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/44.jpg)
![Page 45: Introduction to Java - Clark Science Center · Introduction to Java CSC212 Lecture 7 D. Thiebaut, Fall 2014. More on](https://reader034.vdocuments.site/reader034/viewer/2022050118/5f4f229a7aff8b62173e2053/html5/thumbnails/45.jpg)