Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

* * * * * * * * PLease Solve using java * * * * * * * * File I / O (

******** PLease Solve using java ********
File I/O (Reading)
The purpose of this exercise is to provide you with the opportunity to create command-line 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 command-line applications that are data-driven.
How to handle file paths provided as application input.
How to read, interpret, and resolve errors related to file I/O.
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 hard-codedin other words, the user must be able to enter the path to the input file.
Your code must be able to handle exceptions for I/O issues, like a missing or unreadable input file.
Getting started
Open the file-io-part1-exercises project in IntelliJ.
Open the Java file for the exercise you're working on. The files are in the src/main/java/com/techelevator 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 1.
The tests for this exercise are in the file src/test/java/com/techelevator/WordSearchTests.java. 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 pre-defined 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 Scanner(System.in);. 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 alices_adventures_in_wonderland.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 I/O 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?
[path-to-the-file]
What is the search word you are looking for?
dog
604) conversation. "Are you--are you fond--of--of dogs?" The Mouse did not
605) answer, so Alice went on eagerly: "There is such a nice little dog near
617) won't talk about cats or dogs either, if you don't like them!"
622) history, and you'll understand why it is I hate cats and dogs."
1728)"To begin with," said the Cat, "a dog's not mad. You grant that?"
1732) "Well, then," the Cat went on, "you see a dog growls when it's angry,
When you complete this part, the caseSensitiveSearch_LineNumbers and caseSensitiveSearch_LineText tests now pass.
Step Two: Modify program for case-insensitive word search
Modify the WordSearch program to ask the user if they want the search to be case-sensitive.
Here's an example:
What is the fully qualified name of the file that should be searched?
[path-to-the-file]
What is the search word you are looking for?
Who
Should the search be case sensitive?(Y\N)
Y
204) Who stole the Tarts? 140
518) shall only look up and say, 'Who am I then? Tell me that first, and
1017)"An arm, you goose! Who ever saw one that size? Why, it fills the whole
1042) fancy--Who's to go down the chimney?--Nay, _I_ sha'n't! _You_ do
1169) "Who are _you_?" said the Caterpillar.
1200) "You!" said the Caterpillar contemptuously. "Who are _you_?"
1452) open place, with a little house in it about four feet high. "Whoever
2038) "Who's making personal remarks now?" the Hatter asked triumphantly.
2204) at her, and the Queen said severely, "Who is this?" She said it to the
2371) "Who _are_ you talking to?" said the King, coming up to Alice, and
3042)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions