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);

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter11: Exception Handling
Section: Chapter Questions
Problem 1CP
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

String cashewFileName is read from input. The try block opens a file named cashewFileName to read an integer into dataValue.
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
ReadinputFile.java
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
16
17
cashew1.txt cashew2.txt cashew3.txt
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);
Transcribed Image Text:String cashewFileName is read from input. The try block opens a file named cashewFileName to read an integer into dataValue. 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 ReadinputFile.java 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 16 17 cashew1.txt cashew2.txt cashew3.txt 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);
15
16
17
18
19
20
21
22
23
PPP P
ώ ότι ότ
24
M & N
25
26
27
28
29
30 }
31 }
NN
try {
ReadinputFile.java cashew1.txt cashew2.txt cashew3.txt
cashewInStream = new FileInputStream(cashewFileName);
cashewScanner = new Scanner (cashewInStream);
dataValue = cashewScanner.nextInt();
}
/* Your code goes here */
if (hasErr) {
}
}
else {
System.out.println(cashewFileName + ": File not found");
System.out.println("Value read from + cashewFileName +
"
+ dataValue);
Transcribed Image Text:15 16 17 18 19 20 21 22 23 PPP P ώ ότι ότ 24 M & N 25 26 27 28 29 30 } 31 } NN try { ReadinputFile.java cashew1.txt cashew2.txt cashew3.txt cashewInStream = new FileInputStream(cashewFileName); cashewScanner = new Scanner (cashewInStream); dataValue = cashewScanner.nextInt(); } /* Your code goes here */ if (hasErr) { } } else { System.out.println(cashewFileName + ": File not found"); System.out.println("Value read from + cashewFileName + " + dataValue);
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
  • SEE MORE QUESTIONS
Recommended textbooks for you
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT