JAVA eclipse Can make it exactly like the test case at the bottom. Needs to be e
ID: 3718476 • Letter: J
Question
JAVA eclipse Can make it exactly like the test case at the bottom. Needs to be exactly like the test case I posted at the bottom. Please help!
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.
These are the required output at the bottom.
View required output
Test Case 1
input2.txt
input1.txt
input3.txt
input4.txt
Test Case 2
input2.txt
input1.txt
input3.txt
input4.txt
Test Case 3
input2.txt
input1.txt
input3.txt
input4.txt
Test Case 4
input2.txt
input1.txt
input3.txt
input4.txt
Test Case 5
input2.txt
input1.txt
input3.txt
input4.txt
Test Case 6
input2.txt
input1.txt
input3.txt
input4.txt
Test Case 7
input2.txt
input1.txt
input3.txt
input4.txt
Standard Input Files in the same directoryinput1.txt
input2.txt
input1.txt
input3.txt
input4.txt
Explanation / Answer
ScreenShot
-------------------------------------------------------------------------------------------------------------------------------
Program
/* program to check a file is presnt or not
* If present check first and last names are equal or not
* Display appropriate message
*/
import java.io.*;
import java.util.*;
/* class to read and check names */
public class NameComparison {
public static void main(String [] args) {
/* counter to check count of matches */
int count=0;
/* arraylist to store names in the file */
ArrayList names = new ArrayList();
/* Scanner to read user entered file name */
Scanner sc=new Scanner(System.in);
/* Prompt for The name of the file to open.*/
System.out.println("Please enter a file name or type 'QUIT' to exit:");
String fileName = sc.nextLine();
/* This will reference one line at a time*/
String line = null;
/* loop to check user entered name */
while(!"QUIT".equals(fileName)) {
try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
//Until end of line of files
while((line = bufferedReader.readLine()) != null) {
names.add(line);
}
// Always close files.
bufferedReader.close();
int j=names.size()-1;
//Match loop
for(int i=0;i<names.size()/2;i++) {
if(names.get(i).equals(names.get(j))) {
System.out.println("Match Found:"+names.get(i)+" on line "+(i+1)+" and "+(j));
count++;
}
j--;
}
System.out.println("Total of "+count+" matches found.");
}
//Error display
catch(IOException ex) {
System.out.println(
"File '"
+ fileName + "' is not found.");
}
System.out.println("Please enter a file name or type 'QUIT' to exit:");
// This will reference one line at a time
fileName = sc.nextLine();
}
System.exit(0);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.