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
Code :
package com.geek;
import java.util.*;
public class Finaldasd {
public static void main( String [] args ){
int [][] twoDimArray;
twoDimArray = allocateTwoDimArray();
fillTwoDimArray(twoDimArray);
// printTwoDimArray(twoDimArray);// not shown here
}
public static int [][] allocateTwoDimArray(){
int dim1 = 0, dim2 = 0;
boolean first;
do{
first=false; //boolean variable to check if exception occured
//both nextInt’s in one single try catch block.
try {
//Instead of static scanner, initialize scanner in try block everytime
Scanner scanner = new Scanner(System.in);
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;
} catch (InputMismatchException e) {
// TODO Auto-generated catch block
first=true;
System.out.println(“Enter correct format”); // prompts user again to enter valid input
}
} while(first);
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
Output :