In: Computer Science
Java. Given an input file with each line representing a record of data and the first token (word) being the key that the file is sorted on, we want to load it and output the line number and record for any duplicate keys we encounter. Remember we are assuming the file is sorted by the key and we want to output to the screen the records (and line numbers) with duplicate keys. We are given a text file and have to use the scanner class to lod it
Your task is to
create a FindDuplicates class with the following:
Declaration of an instance variables for the String filename
non-default Constructor - creates an object for user passed filename argument
Accessor methods return the value of each instance variable
Mutator methods that allows th user to set each instance variable (no validation required),
a "getDuplicates()" method that reads from the file (until end-of-file) using Scanner class, finds duplicate records based on the first token on each line (the key), and returns as a String the record number and entire duplicate record one to a line (see above Sample output)
toString() - returns a String message with the value of the instance variable
Sample Output
Enter File Name: input1.txt
FileName:input1.txt
DUPLICATES
12 102380 CS US W 2.8 3.267 125
14 102395 PPCI US W 2.769 2.5 115
25 102567 PPCI US W 3.192 3.412 112
35 102912 CS US Z 3.81 3.667 88
44 103087 CS US Z 2.956 2.688 90
76 103944 CS US W 3.134 3.294 134
77 103944 CS US W 3.698 3.7 94
86 104046 CS US W 2.863 3.133 65
88 104047 CS US W 3.523 3.524 77
89 104047 CS US O 3.825 3.824 49
91 104048 CS US W 3.071 3 94
92 104048 CS US W 3.114 3.111 44
93 104048 CS US W 3.375 3.6 71
Press any key to continue . . .
Please find the required program along with the comments and output:
NOTE: I'm taking only a sample file that is created with the available records only shown in question, and made duplicated some records.
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; class Main { public static void main(String[] args) { String fileName = "C:\\Users\\rishi\\IdeaProjects\\Test\\src\\input1.txt"; //input file path //creating an object for the FindDuplicates class FindDuplicates duplicates = new FindDuplicates(fileName); //get the duplicates String dups = duplicates.getDuplicates(); //print the result System.out.println("DUPLICATES"); System.out.println(dups); } } class FindDuplicates { private String filename; //non-default Constructor - creates an object for user passed filename argument public FindDuplicates(String filename) { this.filename = filename; } //Accessor methods return the value of each instance variable public String getFilename() { return filename; } //Mutator methods that allows th user to set each instance variable public void setFilename(String filename) { this.filename = filename; } public String getDuplicates() { String dups = ""; //to store duplicate records ArrayList<String> keys = new ArrayList<>(); //to store the non duplicate keys try { Scanner scanner = new Scanner(new File(filename)); //scanner to read from file while (scanner.hasNext()) { //read until end of file String line = scanner.nextLine(); //read the current line String[] split = line.split(" "); //split the current line with space String key = split[0].trim(); //get the first word as key if(keys.contains(key)) { //if the key already contains in the list dups = dups + " " + line + "\n"; //if the key is a duplicate, then store the line to return, else continue }else { keys.add(key); //if key is not found in list, then add that key to the list } } }catch (FileNotFoundException fne){ System.out.println("File not found!"); } return dups; } @Override public String toString() { return filename; } }
----------------------------------------
OUTPUT: