In: Computer Science
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.
The code snippet in java with explanation is as below:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class FileRead {
public static void main(String[] args) throws IOException {
// BufferedReader for taking user input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = "";
System.out.println("Enter File name or Quit to exit"); // Asking for user input
input = br.readLine();
while (!input.equalsIgnoreCase("quit")) {
//asking for file names until user enters correct file name or enters quit
List<String> records = new ArrayList<String>(); //ArrayList to store values
try
{
//taking filename with filepath i.e. F:/AB.txt
BufferedReader reader = new BufferedReader(new FileReader(input));
String line;
while ((line = reader.readLine()) != null)
{
records.add(line); //reading names line by line and storing in arrayList
}
if(records.size()==0) //check file size; if empty exit the program
{
System.out.println("File "+ input+" is empty");
break;
}
reader.close();
int countMatch=0,totalRecords=records.size();
for(int i=0;i<records.size()/2;i++)
//checking first half of array with second half for match and traversing only half loop
{
if(records.get(i).equalsIgnoreCase(records.get(records.size()-i-1)))
// check match symmetrically i.e. first with last element second with second last and so on
{
countMatch++;
// increase count by 1everytime there is a match
System.out.println("Match found "+records.get(i)+" at line "+(i+1));
//displaying line number and record when match
}
}
System.out.println("Total Number of matches found is "+countMatch); // Printing the Count of Total matches of name
System.out.println("Total Number of Records in File is "+totalRecords); // Printing total Number of records
input="quit"; //quitting program after the file is found and operation t=ran
}
catch (FileNotFoundException e)
{
/* In case of file not found showing the User file is not found
* and asking h
*/
System.out.println("File "+ input+" is not found");
System.out.println("Enter File name or Quit to exit");
input = br.readLine();
}
catch (Exception e)
{
System.err.format("Exception occurred trying to read '%s'.", input); //Error message for any errors
}
}//end of while
}//end of main
}//end of class
Output
Code
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class FileRead {
public static void main(String[] args)
throws IOException {
// BufferedReader for taking user
input
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
String input = "";
System.out.println("Enter File name
or Quit to exit"); // Asking for
user input
input = br.readLine();
while
(!input.equalsIgnoreCase("quit")) {
//asking for
file names until user enters correct file name or enters quit
List<String> records = new
ArrayList<String>();
//ArrayList to store values
try
{
//taking filename with filepath i.e.
F:/AB.txt
BufferedReader
reader = new BufferedReader(new FileReader(input));
String
line;
while ((line =
reader.readLine()) != null)
{
records.add(line); //reading names
line by line and storing in arrayList
}
if(records.size()==0) //check file
size; if empty exit the program
{
System.out.println("File "+ input+" is
empty");
break;
}
reader.close();
int
countMatch=0,totalRecords=records.size();
for(int i=0;i<records.size()/2;i++)
//checking first half of array with second half
for match and traversing only half loop
{
if(records.get(i).equalsIgnoreCase(records.get(records.size()-i-1)))
// check
match symmetrically i.e. first with last element second with second
last and so on
{
countMatch++;
//
increase count by 1everytime there is a match
System.out.println("Match found "+records.get(i)+" at line
"+(i+1));
//displaying line number and record when match
}
}
System.out.println("Total Number of matches found is
"+countMatch); // Printing the Count
of Total matches of name
System.out.println("Total Number of Records in File is
"+totalRecords); // Printing total Number of
records
input="quit";
//quitting program after the file is found and operation
t=ran
}
catch
(FileNotFoundException e)
{
/* In case of
file not found showing the User file is not found
* and asking
h
*/
System.out.println("File "+ input+" is not found");
System.out.println("Enter File name or Quit to exit");
input = br.readLine();
}
catch (Exception
e)
{
System.err.format("Exception occurred trying to
read '%s'.", input); //Error message for any errors
}
}//end of while
}//end of main
}//end of class