CSC212 Lab 5 Solutions 2014
--D. Thiebaut (talk) 07:26, 28 September 2014 (EDT)
Contents
Lab5Bomb.java
import java.util.Scanner;
public class Lab5Bomb {
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 ) {
System.out.println( "You must enter an integer. Please reenter." );
input.nextLine();
continue;
}
try {
System.out.println( n1 + " / " + n2 + " is " + (n1/n2) );
}
catch ( ArithmeticException e ) {
System.out.println( "Exception: " + e.getMessage() +
"\nReenter numbers." );
}
}
}
}
Lab5ReadFile.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Lab5ReadFile {
public static void main( String[] args ) {
String fileName;
int n1, n2;
Scanner inFile = null;
if ( args.length == 0 ) {
System.out.println( "Syntax: java Lab5ReadFile textFileName" );
System.out.println( " where textFileName is a name of a data file.");
return;
}
fileName = args[0];
try {
inFile = new Scanner( new File( fileName ) );
} catch ( FileNotFoundException e ) {
System.out.println( "File does not exist.");
return;
}
while ( inFile.hasNext() ) {
n1 = inFile.nextInt();
n2 = inFile.nextInt();
System.out.println( n1 + " / " + n2 + " is " + n1/n2 );
}
}
}
Lab5ReadFileThrow2.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Lab5ReadFileThrow2 {
private static Scanner openFile( String fileName ) throws FileNotFoundException {
Scanner inFile;
inFile = new Scanner(new File(fileName));
return inFile;
}
private static int[] get2Ints( Scanner inFile ) {
int n1 = inFile.nextInt();
int n2 = inFile.nextInt();
return new int[] { n1, n2 };
}
public static void main(String[] args) {
Scanner inFile = null;
// if user forgot the enter file name on command line, remind her
if (args.length == 0) {
System.out.println("Syntax: java Lab5ReadFile textFileName");
System.out
.println(" where textFileName is a name of a data file.");
return;
}
// set file name to first argument on command line
try {
inFile = openFile( args[0] );
} catch (FileNotFoundException e) {
System.err.println( "Could not find file " + args[0] );
return;
}
// read the file
while (inFile.hasNext()) {
int[] a = get2Ints( inFile );
System.out.println( a[0] + " / " + a[1] + " is " + a[0] / a[1] );
}
}
}
Lab5ReadFileThrow3.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;
class MyException extends Exception {
MyException( String message ) {
super( message );
}
}
public class Lab5ReadFileThrow3 {
private static Scanner openFile( String fileName )
throws FileNotFoundException {
Scanner inFile;
inFile = new Scanner(new File(fileName));
return inFile;
}
private static int[] get2Ints( Scanner inFile )
throws InputMismatchException, MyException {
int n1 = inFile.nextInt();
int n2 = inFile.nextInt();
if ( n2 == 0 )
throw new MyException( "Invalid 2nd operand: 0\n" );
return new int[] { n1, n2 };
}
public static void main(String[] args) {
Scanner inFile = null;
// if user forgot the enter file name
// on command line, remind her
if (args.length == 0) {
System.out.println("Syntax: java Lab5ReadFile"
+ " textFileName");
System.out.println(" where textFileName "
+ "is a name of a data file.");
return;
}
// set file name to first argument on command line
try {
inFile = openFile( args[0] );
} catch (FileNotFoundException e) {
System.err.println( "Could not find file " + args[0] );
return;
}
// read the file
while (inFile.hasNext()) {
int[] a;
try {
a = get2Ints( inFile );
} catch ( InputMismatchException e ) {
System.out.println( "Wrong number format."+
"Please reenter!" );
inFile.nextLine();
continue;
} catch ( MyException e) {
System.out.println( "Invalid second int:"
+ "0. Skipping this line." );
//inFile.nextLine();
continue;
}
System.out.println( a[0] + " / " + a[1] + " is " + a[0] / a[1] );
}
}
}
Lab5ReadFileThrow.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Lab5ReadFileThrow {
private static Scanner openFile( String fileName ) throws FileNotFoundException {
Scanner inFile;
inFile = new Scanner(new File(fileName));
return inFile;
}
public static void main(String[] args) {
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 Lab5ReadFile textFileName");
System.out
.println(" where textFileName is a name of a data file.");
return;
}
// set file name to first argument on command line
try {
inFile = openFile( args[0] );
} catch (FileNotFoundException e) {
System.err.println( "Could not find file " + args[0] );
return;
}
// read the file
while (inFile.hasNext()) {
n1 = inFile.nextInt();
n2 = inFile.nextInt();
System.out.println(n1 + " / " + n2 + " is " + n1 / n2);
}
}
}