Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

String fileName is read from input. The try block opens a file named fileName using a try - with - resources statement, and reads an

String fileName is read from input. The try block opens a file named fileName 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 hasError with true. 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 fileName;
int inValue;
boolean hasError = false;
fileName = scnr.next();
try (Scanner fScanner = new Scanner(new FileInputStream(fileName))){
inValue = fScanner.nextInt();
System.out.println("Value read from "+ fileName +": "+ inValue);
}
/* Your code goes here */
/* fScanner is automatically closed after the try block terminates. */
if (hasError){
System.out.println(fileName +": File not found");
}
else {
System.out.println("Success: File processed");
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Medical Image Databases

Authors: Stephen T.C. Wong

1st Edition

1461375398, 978-1461375395

More Books

Students also viewed these Databases questions

Question

Want to put your expertise in the spotlight?

Answered: 1 week ago

Question

7. It is advisable to do favors for people whenever possible.

Answered: 1 week ago

Question

9. Power and politics can be destructive forces in organizations.

Answered: 1 week ago