Question: String papayaDataName is read from input. The try block opens a file named papayaDataName to read an integer into papayaData. Write an exception handler to

String papayaDataName is read from input. The try block opens a file named papayaDataName to read an integer into papayaData.
Write an exception handler to catch a FileNotFoundException. In the catch block, output papayaDataName, followed by ": File not found". End with a newline.
After the catch block, close fInScanner if fInScanner is open. import java.util.Scanner;
import java.io.FileNotFoundException;
// This program uses an overloaded FileInputStream library to check for file closure
public class ReadInputFile {
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
FileInputStream papayaDataStream;
Scanner fInScanner = null;
String papayaDataName;
int papayaData;
papayaDataName = scnr.next();
try {
papayaDataStream = new FileInputStream(papayaDataName);
fInScanner = new Scanner(papayaDataStream);
papayaData = fInScanner.nextInt();
System.out.println("Value read from "+ papayaDataName +": "+ papayaData);
}
/* Your code goes here */
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!