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);
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
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.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F0cc153ae-b205-4fb2-9991-7cf6a21d5016%2F85034e95-9448-4ac1-a37c-42c5cdf5c741%2F7wj7qqd_processed.png&w=3840&q=75)
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.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F0cc153ae-b205-4fb2-9991-7cf6a21d5016%2F85034e95-9448-4ac1-a37c-42c5cdf5c741%2Ffu3gx5a_processed.png&w=3840&q=75)
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
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Knowledge Booster
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.Recommended textbooks for you
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
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)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
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)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education