Question: CHALLENGE ACTIVITY 4 . 3 . 1 : Exceptions with files. 5 2 2 5 6 2 3 5 7 8 1 9 4 quazay?

CHALLENGE
ACTIVITY
4.3.1: Exceptions with files.
5225623578194 quazay?
Jump to level 1
String spinachDataName is read from input. The try block opens a file named spinachDataName using a try-with-resources statement, and reads an integer from the file into inValue.
Write an exception handler to catch a FileNotFoundException and assign hasErr with true.
Ex: If the input is spinach1.txt, then the output is:
Value read from spinachl.txt: 40
Success: File processed
Ex. If the input is nothing. txt, then the output is:
nothing.txt: Error occurred
\table[[spinach2.txt,spinach3.txt]]
import java.util. Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class ReadDatafile {
public static void main(String args){
Scanner scnr = new Scanner(
System.in);
String spinachDataName;
int inValue;
boolean hasErr = false;
spinachDataName = scnr. next();
try (Scanner spinachScanner = new Scanner(new FileInputStream(spinachDataName))){
inValue = spinachScanner .nextInt ();
 CHALLENGE ACTIVITY 4.3.1: Exceptions with files. 5225623578194 quazay? Jump to level

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!