Question: Please write in Java Question 1 Write a program that prompts the user to enter a file name, then opens the file in text mode

Please write in Java

Question 1

Write a program that prompts the user to enter a file name, then opens the file in text mode and reads names. The file contains one name on each line. The program then compares each name with the name that is at the end of the file in a symmetrical position. For example if the file contains 10 names, the name #1 is compared with name #10, name #2 is compared with name #9, and so on. If you find matches you should print the name and the line numbers where the match was found.

While entering the file name, the program should allow the user to type quit to exit the program.

If the file with a given name does not exist, then display a message and allow the user to re-enter the file name.

The file may contain up to 100 names.

You can use an array or ArrayList object of your choosing, however you can only have one array or ArrayList.

Input validation:

a) If the file does not exist, then you should display a message "File 'somefile.txt' is not found." and allow the user to re-enter the file name.

b) If the file is empty, then display a message "File 'somefile.txt' is empty." and exit the program.

Hints:

a) Perform file name input validation immediately after the user entry and use a while loop .

b) Use one integer variable to count names in the file and another one for counting matches.

d) You can use either a while loop or a for loop to find the matches.

Grading guide:

1. Functionality: Does the program work and perform basic functionality as per requirements? (Need to be able to compile and run the program with some input and some output): 40%

2. Correctness: Does the program properly count matches? - 10%

3. Input Validation: Is the user input validated against requirements? - 10%

4. Output Formatting: Is the output formatted according to the requirements: 10%

5. Edge Cases: Does the program work without errors for ALL values of input (missing file, empty file, odd or even number of lines, very large file, etc.) - 10%

Required Output

Test Case 1

Standard Input Files in the same directoryinput1.txt 

input2.txt

input1.txt

input3.txt

input4.txt

Please enter the file name or type QUIT to exit: Match found: 'Michael' on lines 1 and 8. Match found: 'Cassandra' on lines 3 and 6. Total of 2 matches found. 

Test Case 2

Standard Input Files in the same directoryinput2.txt 

input2.txt

input1.txt

input3.txt

input4.txt

Please enter the file name or type QUIT to exit: Match found: 'Michael' on lines 1 and 9. Match found: 'Cassandra' on lines 3 and 7. Total of 2 matches found. 

Test Case 3

Standard Input Files in the same directoryinput3.txt 

input2.txt

input1.txt

input3.txt

input4.txt

Please enter the file name or type QUIT to exit: File 'input3.txt' is empty. 

Test Case 4

Standard Input Files in the same directoryinput4.txt 

input2.txt

input1.txt

input3.txt

input4.txt

Please enter the file name or type QUIT to exit: Match found: 'Michael' on lines 1 and 16. Match found: 'Joshua' on lines 2 and 15. Match found: 'Cassandra' on lines 3 and 14. Match found: 'Joseph' on lines 4 and 13. Match found: 'William' on lines 5 and 12. Match found: 'Matthew' on lines 6 and 11. Match found: 'James' on lines 7 and 10. Match found: 'Steven' on lines 8 and 9. Total of 8 matches found. 

Test Case 5

Standard Input Files in the same directoryinput5.txtENTER input1.txt 

input2.txt

input1.txt

input3.txt

input4.txt

Please enter the file name or type QUIT to exit: File 'input5.txt' is not found. Please re-enter the file name or type QUIT to exit: Match found: 'Michael' on lines 1 and 8. Match found: 'Cassandra' on lines 3 and 6. Total of 2 matches found. 

Test Case 6

Standard Input Files in the same directoryqUiT 

input2.txt

input1.txt

input3.txt

input4.txt

Please enter the file name or type QUIT to exit: 

Test Case 7

Standard Input Files in the same directoryinput5.txtENTER quit 

input2.txt

input1.txt

input3.txt

input4.txt

Please enter the file name or type QUIT to exit: File 'input5.txt' is not found. Please re-enter the file name or type QUIT to exit: 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!