String cashewFileName is read from input. The try block opens a file named cashewFileName to read an integer into dataVal 1. Write an exception handler to catch a FileNotFoundException and assign hasErr with true. 2. After the catch block, close cashewScanner if cashewScanner is open. Ex: If the input is cashew2.txt, then the output is: Value read from cashew2.txt: 21 Ex: If the input is nothing.txt, then the output is: nothing.txt: File not found 1 import java.util.Scanner; 2 import java.io.FileNotFoundException; 3 // This program uses an overloaded FileInputStream Library to check for file closure 4 5 public class ReadInputFile { 6 7 8 9 10 11 12 13 14 15 4005 16 17 ReadinputFile.java public static void main(String[] args) { Scanner scnr = new Scanner(System.in); FileInputStream cashewInStream; Scanner cashewScanner = null; String cashewFileName; int dataValue = -1; boolean hasErr = false; cashewFileName try { cashew1.txt cashew2.txt cashew3.txt = scnr.next(); cashewInStream = new FileInputStream(cashewFileName);

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

ONLY ReadInputFile.java file can be edited with New JAVA CODE

ONLY new code can be added between 21 and 23 (as seen in image).

 

cashew1.txt

47

 

cashew2.txt

21

 

cashew3.txt

25

### Reading from a File in Java

This example demonstrates how to read an integer from a file using Java, and how to properly handle exceptions such as `FileNotFoundException`.

#### Key Steps

1. **Reading the File Name:**
   - The file name `cashewFileName` is read from user input.

2. **Exception Handling:**
   - Implement an exception handler to catch `FileNotFoundException` and set `hasErr` to true when an error occurs.
   - Ensure the file scanner (`cashewScanner`) is closed after reading.

#### Examples

- **Successful Reading:**
  - Input: `cashew2.txt`
  - Output: `Value read from cashew2.txt: 21`

- **File Not Found:**
  - Input: `nothing.txt`
  - Output: `nothing.txt: File not found`

#### Java Code Explanation

Below is the Java code used to achieve the above functionality:

```java
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 cashewInStream;
        Scanner cashewScanner = null;
        String cashewFileName;
        int dataValue = -1;
        boolean hasErr = false;
        
        cashewFileName = scnr.next();
        
        try {
            cashewInStream = new FileInputStream(cashewFileName);
```

#### Diagram Overview

The accompanying code provides the functionality to:

- Take a file name input from the user.
- Attempt to read from the specified file.
- Handle errors gracefully, ensuring that resources are managed appropriately, such as closing the file scanner, if opened.

The tabs below the code section suggest files (`cashew1.txt`, `cashew2.txt`, etc.) that might contain integer data to be read by the program.
Transcribed Image Text:### Reading from a File in Java This example demonstrates how to read an integer from a file using Java, and how to properly handle exceptions such as `FileNotFoundException`. #### Key Steps 1. **Reading the File Name:** - The file name `cashewFileName` is read from user input. 2. **Exception Handling:** - Implement an exception handler to catch `FileNotFoundException` and set `hasErr` to true when an error occurs. - Ensure the file scanner (`cashewScanner`) is closed after reading. #### Examples - **Successful Reading:** - Input: `cashew2.txt` - Output: `Value read from cashew2.txt: 21` - **File Not Found:** - Input: `nothing.txt` - Output: `nothing.txt: File not found` #### Java Code Explanation Below is the Java code used to achieve the above functionality: ```java 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 cashewInStream; Scanner cashewScanner = null; String cashewFileName; int dataValue = -1; boolean hasErr = false; cashewFileName = scnr.next(); try { cashewInStream = new FileInputStream(cashewFileName); ``` #### Diagram Overview The accompanying code provides the functionality to: - Take a file name input from the user. - Attempt to read from the specified file. - Handle errors gracefully, ensuring that resources are managed appropriately, such as closing the file scanner, if opened. The tabs below the code section suggest files (`cashew1.txt`, `cashew2.txt`, etc.) that might contain integer data to be read by the program.
This image displays a code editor with a Java program named `ReadInputFile.java`. The editor also shows three text files: `cashew1.txt`, `cashew2.txt`, and `cashew3.txt`. Below is the transcribed code content visible in the image:

```java
15 try {
16     cashewInStream = new FileInputStream(cashewFileName);
17     cashewScanner = new Scanner(cashewInStream);
18     dataValue = cashewScanner.nextInt();
19 }
20 /* Your code goes here */
21
22 if (hasErr) {
23     System.out.println(cashewFileName + ": File not found");
24 }
25 else {
26     System.out.println("Value read from " + cashewFileName + ": " + dataValue);
27 }
28 }
29 }
```

### Explanation of Code Structure:

- **Lines 15-19**: This block is a `try` block that attempts to open a file using a `FileInputStream` and reads an integer from it using a `Scanner`.

- **Line 20**: A comment line indicating where to add additional code if necessary.

- **Lines 22-27**: This section handles the condition `if (hasErr)` to check if an error, such as a file not being found, exists. If true, it prints a file not found message. Otherwise, it prints the value read from the file.

This code is likely part of a program that reads integer values from specified text files and handles exceptions if the file is not found.
Transcribed Image Text:This image displays a code editor with a Java program named `ReadInputFile.java`. The editor also shows three text files: `cashew1.txt`, `cashew2.txt`, and `cashew3.txt`. Below is the transcribed code content visible in the image: ```java 15 try { 16 cashewInStream = new FileInputStream(cashewFileName); 17 cashewScanner = new Scanner(cashewInStream); 18 dataValue = cashewScanner.nextInt(); 19 } 20 /* Your code goes here */ 21 22 if (hasErr) { 23 System.out.println(cashewFileName + ": File not found"); 24 } 25 else { 26 System.out.println("Value read from " + cashewFileName + ": " + dataValue); 27 } 28 } 29 } ``` ### Explanation of Code Structure: - **Lines 15-19**: This block is a `try` block that attempts to open a file using a `FileInputStream` and reads an integer from it using a `Scanner`. - **Line 20**: A comment line indicating where to add additional code if necessary. - **Lines 22-27**: This section handles the condition `if (hasErr)` to check if an error, such as a file not being found, exists. If true, it prints a file not found message. Otherwise, it prints the value read from the file. This code is likely part of a program that reads integer values from specified text files and handles exceptions if the file is not found.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Exception Handling Keywords
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education