Consider the following method:
public static void printLine(String line, int numberOfTimes) throws Exception {
if (numberOfTimes <=0)
throw new ParameterNotAllowedException("Error. invalid parameter.", numberOFTimes);
for( int i=0; i System.out.println(line);
}
Consider the following main method which asks the user to enter a file name. The program then attempts to open the file. If successful, the program will read in a line and an integer and will make a call to the above method. The following method does no exception handling. Please correct the method so that all Exceptions potentially thrown are handled differently.
import java.util.*;
import java.io.*;
public class Question {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter a file name: ");
String name = keyboard.next();
Scanner file = new Scanner(new File(name));
String line = file.nextLine();
int n = file.nextInt();
printLine(line, n);
}
//method from above goes here.
}

Respuesta :

Answer:

See explaination

Explanation:

public class Main {

public static void main(String[] args) {

//execution starts from here so initializing the try block for erros

try{

Scanner keyboard = new Scanner(System.in);

System.out.print("Please enter a file name: ");

String name = keyboard.next();

//the line below throws file not found exception if the given file name is not found or unable to open

Scanner file = new Scanner(new File(name));

String line = file.nextLine();

int n = file.nextInt();

//the below print line function call throws ParameterNotAllowedException if n is zero

printLine(line, n);

}//all Exceptions potentially thrown

catch(InputMismatchException e){

System.out.println("User input type mismatch\n"+e);

}catch(FileNotFoundException e){

System.out.println("Given file name is wrong or does not match");

}catch(ParameterNotAllowedException e){

System.out.println("The n given to printLine must be more than zero");

}catch(IOException ex){

System.err.println("An IOException was caught!");

}

}

//method from above goes here.

public static void printLine(String line, int numberOfTimes) throws Exception {

if (numberOfTimes <=0)

throw new ParameterNotAllowedException("Error. invalid parameter.",numberOFTimes);

for( int i=0; i<numberOfTimes; i++)

System.out.println(line);

}

}

For the user written custom Exception ParameterNotAllowedException. We need to add an exceptional class.

Which is missing from the given info:

class ParameterNotAllowedException extends Exception

{

public ParameterNotAllowedException(String s,int n)

{

// Call constructor of parent Exception

super(s);

}

}

Answer:

Check the explanation

Explanation:

public class Main {

public static void main(String[] args) {

//execution starts from here so initializing the try block for errors

try{

Scanner keyboard = new Scanner(System.in);

System.out.print("Please enter a file name: ");

String name = keyboard.next();

//the line below throws file not found exception if the given file name is not found or unable to open

Scanner file = new Scanner(new File(name));

String line = file.nextLine();

int n = file.nextInt();

//the below print line function call throws ParameterNotAllowedException if n is zero

printLine(line, n);

}//all Exceptions potentially thrown

catch(InputMismatchException e){

System.out.println("User input type mismatch\n"+e);

}catch(FileNotFoundException e){

System.out.println("Given file name is wrong or does not match");

}catch(ParameterNotAllowedException e){

System.out.println("The n given to printLine must be more than zero");

}catch(IOException ex){

System.err.println("An IOException was caught!");

}

}

//method from above goes here.

public static void printLine(String line, int numberOfTimes) throws Exception {

if (numberOfTimes <=0)

throw new ParameterNotAllowedException("Error. invalid parameter.",numberOFTimes);

for( int i=0; i<numberOfTimes; i++)

System.out.println(line);

}

}

For the user written custom Exception ParameterNotAllowedException. We need to add an exceptional class.

Which is missing from the given info:

class ParameterNotAllowedException extends Exception

{

public ParameterNotAllowedException(String s,int n)

{

// Call constructor of parent Exception

super(s);

}

}