Generates random multiplication problems (intended for practice)
@author
@version
import java.util.Scanner;
import java.util.Random;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
public class MathPractice{
final static int NUMBER_OF_PROBLEMS = 20;
public static void main( String[] args ){
Scanner stdinScanner = new Scanner( System.in );
String filename;
int num1;
int num2;
FileWriter fw = null;
BufferedWriter bw = null;
PrintWriter pw = null;
Random rand = new Random();
boolean foundError = false;
try{
System.out.print("Please enter the output filename: ");
filename = stdinScanner.next();
fw = new FileWriter( filename );
bw = new BufferedWriter( fw );
pw = new PrintWriter( bw );
}catch( InputMismatchException e ){
System.err.println("Sorry, there was an error parsing your input!" );
return;
}catch( FileNotFoundException e ){
System.err.println("Sorry, that was not a valid filename: " + e.getMessage() );
return;
}catch( Exception e ){
System.err.println("ERROR: " + e.getMessage() );
return;
}
for( int i = 0; i < NUMBER_OF_PROBLEMS && !foundError; i++){
num1 = rand.nextInt( 10 );
num2 = rand.nextInt( 10 );
pw.println( num1 + " * " + num2 + " = ");
if( pw.checkError() ){
System.err.println("Write to file failed!" );
foundError = true;
}
}
pw.close();
}
}