Determine which exception will be thrown by the Scanner class’ nextInt() method. Change The CODE。 Put both of the nextInt() calls (in my allocate2DimArray method) in a try block and catch the specific exception that nextInt() would throw if the user entered non-integer input. In the catch clause, display an error message. Use a loop to have the user to input both numbers again if an exception is caught. DON’T use recursion, but put the try block and catch clause inside a loop, using a boolean variable that will be tested in the loop condition (see example). (Optional: also catch an exception that would be thrown if the array was allocated with negative sizes)
CODE
import java.util.*;
public class Exercise4_2 {
static Scanner scanner = new Scanner(System.in);
public static void main( String [] args ){
int [][] twoDimArray;
twoDimArray = allocateTwoDimArray();
fillTwoDimArray(twoDimArray);
printTwoDimArray(twoDimArray);// not shown here
} // end main
public static int [][] allocateTwoDimArray(){
int dim1, dim2;
System.out.print(“Enter number of rows: “);
dim1 = scanner.nextInt();
if( dim1 < 1 )
dim1 = 1;
System.out.print(“Enter number of columns: “);
dim2 = scanner.nextInt();
if( dim2 < 1 )
dim2 = 1;
return new int [dim1][dim2];
} // end allocateTwoDimArray
public static void fillTwoDimArray(int [][] twoDimArray){
if( twoDimArray != null){
for( int row=0; row < twoDimArray.length; ++row ){
for( int col=0; col < twoDimArray[row].length; ++col ){
twoDimArray[row][col]=10*row+col;
} // end for col
} // end for row
}
} // end fillTwoDimArray
// print method not shown here
} // end class TryTwoDimArrays
Example:
//be sure to import java.util.* for ArrayList
ArrayList<String> list = new ArrayList<String>();
String str; int i; boolean errorOccurred;
// other code may be here
do {
errorOccurred = false;
try{
// code here to assign to i
str=list.get(i);// might throw an exception
// put code here for when there’s NO exception
}
catch(IndexOutOfBoundsException exp){
errorOccurred=true;
System.err.println(“Exception: ” + exp);
// should do more to “handle” the exception here
} // end catch exception
}while( errorOccurred);
Expert Answer
import java.util.*;
class NegativeDimensions extends Exception {
public NegativeDimensions(String msg) {
super(msg);
}
}
public class Exercise4_2 {
static Scanner scanner = new Scanner(System.in);
public static void main( String [] args ){
int [][] twoDimArray;
twoDimArray = allocateTwoDimArray();
fillTwoDimArray(twoDimArray);
printTwoDimArray(twoDimArray);// not shown here
} // end main
public static int [][] allocateTwoDimArray(){
int dim1=0, dim2=0;
boolean invalid_input = false;
do {
invalid_input = false;
System.out.print(“Enter number of rows: “);
try {
dim1 = scanner.nextInt();
if(dim1 < 0) {
throw new NegativeDimensions(“Number of rows cannot be negative”);
}
} catch(InputMismatchException ime) {
System.out.println(“InputMismatchException: That was not an integer. Try again.”);
invalid_input = true;
continue;
} catch(NegativeDimensions e) {
System.out.println(e.toString() + ” Try again.”);
invalid_input = true;
continue;
}
if( dim1 < 1 )
dim1 = 1;
System.out.print(“Enter number of columns: “);
try {
dim2 = scanner.nextInt();
if(dim2 < 0) {
throw new NegativeDimensions(“number of columns cannot be negative”);
}
} catch(InputMismatchException ime) {
System.out.println(“InputMismatchException: That was not an integer. Try again.”);
invalid_input = true;
continue;
} catch(NegativeDimensions e) {
System.out.println(e.toString() + ” Try again.”);
invalid_input = true;
continue;
}
if( dim2 < 1 )
dim2 = 1;
} while (invalid_input);
return new int [dim1][dim2];
} // end allocateTwoDimArray
public static void fillTwoDimArray(int [][] twoDimArray){
if( twoDimArray != null){
for( int row=0; row < twoDimArray.length; ++row ){
for( int col=0; col < twoDimArray[row].length; ++col ){
twoDimArray[row][col]=10*row+col;
} // end for col
} // end for row
}
} // end fillTwoDimArray
// print method not shown here
} // end class TryTwoDimArrays