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
: Exceptions with files.
quazay?
Jump to level
String spinachDataName is read from input. The try block opens a file named spinachDataName using a trywithresources 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 spinachtxt then the output is:
Value read from spinachl.txt:
Success: File processed
Ex If the input is nothing. then the output is:
nothing.txt: Error occurred
tablespinachtxtspinachtxt
import java.util. Scanner;
import java.ioFileInputStream;
import java.ioFileNotFoundException;
public class ReadDatafile
public static void mainString args
Scanner scnr new Scanner
System.in;
String spinachDataName;
int inValue;
boolean hasErr false;
spinachDataName scnr next;
try Scanner spinachScanner new Scannernew FileInputStreamspinachDataName
inValue spinachScanner nextInt ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
