Answered step by step
Verified Expert Solution
Question
1 Approved Answer
* * * * * * * * PLease Solve using java * * * * * * * * File I / O (
PLease Solve using java
File IO Reading
The purpose of this exercise is to provide you with the opportunity to create commandline applications that can read and analyze files.
Learning objectives
After completing this exercise, you'll understand:
How to read data from a text file.
How to create complex, interactive commandline applications that are datadriven.
How to handle file paths provided as application input.
How to read, interpret, and resolve errors related to file IO
Evaluation criteria and functional requirements
The project must not have any build errors.
The application returns the expected results.
The unit tests pass as expected.
Note: Tests are only provided for the WordSearch exercise.
Paths to the input files aren't hardcodedin other words, the user must be able to enter the path to the input file.
Your code must be able to handle exceptions for IO issues, like a missing or unreadable input file.
Getting started
Open the fileiopartexercises project in IntelliJ.
Open the Java file for the exercise you're working on The files are in the srcmainjavacomtechelevator package.
Provide enough code to get the program started.
Verify your work on the command line.
Repeat until you've implemented all required features and all unit tests pass.
Part One: WordSearch program
In this exercise, you'll write a program that searches the contents of a file for a word. For each occurrence of the word in the file, you'll display the line number and the contents of that line in the console.
NOTE: Line numbers begin with
The tests for this exercise are in the file srctestjavacomtechelevatorWordSearchTestsjava. If you run these tests before working on the exercise, all tests fail. As you complete each step, more tests pass.
Processing user input
A predefined Scanner object to read user input is available in each class of the exercise. It's best practice to use one Scanner for reading user input in Java applications.
Before the main method, you'll see private final Scanner userInput new ScannerSystemin; You can use this Scanner in your code to read input from the keyboard.
Step One: Collect user input and perform search
When the program runs, it must first prompt the user for a filesystem path and a word to search for in the file.
A file called alicesadventuresinwonderland.txt is in your project folder that you can use as test input. After collecting the required information from the user, you can search through the file for all occurrences of the search word and output that to the console.
For each matching line, your output must contain the line number and the text of the matching line, separated by one or more spaces.
Optionally, you may also use one of the following separator characters between the line number and the text of the matching line, as long as there is some space before the line text:
:
While reading the file, be sure to handle any IO exceptions that may occur. Display a message to inform the user about the error. Remember that it's never a good idea to display the raw exception message to the user.
Here's an example of what your application could look like:
What is the fully qualified name of the file that should be searched?
pathtothefile
What is the search word you are looking for?
dog
conversation. "Are youare you fondofof dogs?" The Mouse did not
answer, so Alice went on eagerly: "There is such a nice little dog near
won't talk about cats or dogs either, if you don't like them!"
history, and you'll understand why it is I hate cats and dogs."
To begin with," said the Cat, "a dog's not mad. You grant that?"
"Well, then," the Cat went on "you see a dog growls when it's angry,
When you complete this part, the caseSensitiveSearchLineNumbers and caseSensitiveSearchLineText tests now pass.
Step Two: Modify program for caseinsensitive word search
Modify the WordSearch program to ask the user if they want the search to be casesensitive
Here's an example:
What is the fully qualified name of the file that should be searched?
pathtothefile
What is the search word you are looking for?
Who
Should the search be case sensitiveYN
Y
Who stole the Tarts?
shall only look up and say, 'Who am I then? Tell me that first, and
An arm, you goose! Who ever saw one that size? Why, it fills the whole
fancyWho's to go down the chimney?Nay, I sha'n't! You do
"Who are you said the Caterpillar.
"You!" said the Caterpillar contemptuously. "Who are you
open place, with a little house in it about four feet high. "Whoever
"Who's making personal remarks now?" the Hatter asked triumphantly.
at her, and the Queen said severely, "Who is this?" She said it to the
"Who are you talking to said the King, coming up to Alice, and
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started