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);
Chapter11: Exception Handling
Section: Chapter Questions
Problem 1CP
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
Expert Solution
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
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
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT