Question: String volumeDataFile is read from input. The try block opens a file named volumeDataFile to read a floating - point number into valueRead. Write an

String volumeDataFile is read from input. The try block opens a file named volumeDataFile to read a floating-point number into valueRead.
Write an exception handler to catch a FileNotFoundException. In the catch block, output volumeDataFile, followed by ": Error occurred". End with a newline.
After the catch block, close fileScanner if fileScanner is open.
Click here for examples
Ex 1: If the input is volume2.txt, then the output is:
Value read from volume2.txt: 1.7
Ex 2: If the input is nothing.txt, then the output is:
nothing.txt: Error occurred
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 volumeFStream;
Scanner fileScanner = null;
String volumeDataFile;
double valueRead;
volumeDataFile = scnr.next();
try {
volumeFStream = new FileInputStream(volumeDataFile);
fileScanner = new Scanner(volumeFStream);
valueRead = fileScanner.nextDouble();
System.out.println("Value read from "+ volumeDataFile +": "+ valueRead);
}
/* 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 Finance Questions!