CSCI-1320 Lab: Files I

Instructor: Todd Dole

Purpose Statement

This assignment gives practice reading data from text files.

Instructions

Create a class called SearchFile with a main method which accepts two inputs from the user: The path of a text file (e.g. "kjv.txt") and a search string.

The program should open the specified file, read the contents, and print all lines that include the search string.

You can use the String class's .contains() method to check if a line contains the text:

if (myString.contains("text")) System.out.println("text found!"); //

You may download the King James Version of the Bible as a text file for testing purposes. (Right click the link and choose "Save link as..." You may want to save it to the same folder where your java code is located.)

Example Output:

                Please enter a file to search:  kjv.txt
                Please enter the text to search for:  Ichabod
                Results:
                    1 Samuel 4:21	And she named the child Ichabod, saying, The glory is departed from Israel: because the ark of God was taken, and because of her father in law and her husband.
                    1 Samuel 14:3	And Ahiah, the son of Ahitub, Ichabod’s brother, the son of Phinehas, the son of Eli, the LORD’S priest in Shiloh, wearing an ephod. And the people knew not that Jonathan was gone.
        

Once you have your basic search working, use toLowerCase() or toUpperCase() to make it so that the search works regardless of case (case insensitive). The above result should be the same if the user searched for "Ichabod" or "ichabod"

Also, be sure to include a try-catch block in case the user enters a file name that does not exist. Your program should handle this gracefully.

Submit your working SearchFile.java code via Canvas.