Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do i get this program to read and sort this file? File: 10.txt 10 Stocker No

ID: 3698330 • Letter: H

Question

How do i get this program to read and sort this file?

File:

10.txt

10
Stocker Norris | 3631 W. 8th Ln. #264 | TRUCKEE | CA | 96161
Saylor Alisya | 1854 W. Green Ln. #89 | LITTLE SUAMICO | WI | 54141
Moore Lucy | 1471 E. Forest Rd. #139 | TALMAGE | KS | 67482
Ryals Larson | 8385 E. Hillcrest St. | LEWISTON | MN | 55952
Sherill Hardy | 7313 N. Franklin Blvd. | TAHLEQUAH | OK | 74464
Justin Ray | 9251 N. Cherry Way #221 | MAMARONECK | NY | 10543
Justin Ray | 4357 N. Broadway Way | LAKELAND | LA | 70752
Earhart Alanis | 7515 N. Washington Way #72 | LOUISE | TX | 77455
Draudy Joleen | 201 NE 23rd St. | HOUSTON | TX | 77061
Bishop Holden | PO Box 409 | CARONA | KS | 66773

Program:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Map;
import static java.util.Map.Entry;

public class Pgm1 {

    public static void main(String... args) throws Exception {
      
        Sorter<Record> sorter = new InsertionsortSorter();
      
        if (args.length != 1) {
            System.out.println("Invalid Usage -- please supply a filename as an argument");
            System.out.println("`java -jar pgm1-...jar 10.txt`");
        } else {
            String filename = args[0];
            //we have a filename, make sure file exists
            File file = new File(filename);
            if (!file.exists()) {
                System.out.println("The file you supplied doesn't seem to exist!");
            } else {
                //start processing
                System.out.println("Found file " + filename);

                String filename_number = filename.split("\.")[0];

                RecordSorter recordSorter = new RecordSorter();

                String nameFileName = "name_" + filename;
                String lNameFileName = "lName_" + filename;
                Map<String, String> lNameSortPerformance = recordSorter.sortRecordsFromFileByKeyWithOutputFile(
                        filename,
                        new LastNameComparator(),
                        sorter,
                        lNameFileName);
              
                String fNameFileName = "fName_" + filename;
                Map<String, String> fNameSortPerformance = recordSorter.sortRecordsFromFileByKeyWithOutputFile(
                        lNameFileName,
                        new FirstNameComparator(),
                        sorter,
                        nameFileName);
              
                new File(lNameFileName).delete();


                File namePerformanceFile = new File("name_" + filename_number + "_time.txt");

                if (!namePerformanceFile.exists()) {
                    namePerformanceFile.createNewFile();
                }

                BufferedWriter bw = new BufferedWriter(new FileWriter(namePerformanceFile));
                for (Entry<String, String> entry : fNameSortPerformance.entrySet()) {
                    bw.write(String.format("%6s %s ", entry.getKey(), entry.getValue()));
                }

                try {
                    bw.close();
                } catch (Exception e) {
                    System.out.println("Failed to close buffered writer");
                }


                String addressFileName = "address_" + filename;
                String stateFileName = "state_address_" + filename;
                String cityFileName = "city_address_" + filename;
                String zipFileName = "zip_address_" + filename;
              
                Map<String, String> stateSortPerformance = recordSorter.sortRecordsFromFileByKeyWithOutputFile(
                        nameFileName,
                        new StateComparator(),
                        sorter,
                        stateFileName);
              
                Map<String, String> citySortPerformance = recordSorter.sortRecordsFromFileByKeyWithOutputFile(
                        stateFileName,
                        new CityComparator(),
                        sorter,
                        cityFileName);
              
                Map<String, String> zipSortPerformance = recordSorter.sortRecordsFromFileByKeyWithOutputFile(
                        cityFileName,
                        new ZipcodeComparator(),
                        sorter,
                        zipFileName);
              
                Map<String, String> addressSortPerformance = recordSorter.sortRecordsFromFileByKeyWithOutputFile(
                        zipFileName,
                        new AddressComparator(),
                        sorter,
                        addressFileName);
              
                new File(stateFileName).delete();
                new File(cityFileName).delete();
                new File(zipFileName).delete();

                File addressPerformanceFile = new File("address_" + filename_number + "_time.txt");

                if (!addressPerformanceFile.exists()) {
                    addressPerformanceFile.createNewFile();
                }

                bw = new BufferedWriter(new FileWriter(addressPerformanceFile));
                for (Entry<String, String> entry : addressSortPerformance.entrySet()) {
                    bw.write(String.format("%6s %s ", entry.getKey(), entry.getValue()));
                }

                try {
                    bw.close();
                } catch (Exception e) {
                    System.out.println("Failed to close buffered writer");
                }
            }
        }
    }
}


CityComparator.java

import java.util.Comparator;

public class CityComparator implements Comparator<Record> {

    public int compare(Record a, Record b) {
        if (a == null && b == null) {
            return 0;
        } else if ((a == null && b != null)
                || (a != null && b == null)) {
            return a == null ? 1 : -1;
        } else {
            return a.city.compareToIgnoreCase(b.city);
        }
    }
}


ZipcodeComparator.java

import java.util.Comparator;

public class ZipcodeComparator implements Comparator<Record> {

    public int compare(Record a, Record b) {
        if (a == null && b == null) {
            return 0;
        } else if ((a == null && b != null)
                || (a != null && b == null)) {
            return a == null ? 1 : -1;
        } else {
            int recordCompare = 0;
            if (a.zipcode > b.zipcode) {
                recordCompare = 1;
            } else if (a.zipcode < b.zipcode) {
                recordCompare = -1;
            }

            return recordCompare;
        }
    }
}


Record.java

public class Record {
    public final String lastName;
    public final String firstName;
    public final String address;
    public final String city;
    public final String state;
    public final int zipcode;

    public Record(String delimitedString) {
        String[] parts = delimitedString.split(" \| ");
        String name = parts[0];
        String[] nameParts = name.split(" ");
        firstName = nameParts[0];
        lastName = nameParts[1];
        address = parts[1];
        city = parts[2];
        state = parts[3];
        zipcode = Integer.parseInt(parts[4]);
    }

    public Record(final String lastName,
        final String firstName,
        final String address,
        final String city,
        final String state,
        final int zipcode){
        this.lastName = lastName;
        this.firstName = firstName;
        this.address = address;
        this.city = city;
        this.state = state;
        this.zipcode = zipcode;
    }

    public String toDelimitedString() {
        return new StringBuilder()
        .append(firstName).append(" ").append(lastName)
        .append(" | ")
        .append(address)
        .append(" | ")
        .append(city)
        .append(" | ")
        .append(state)
        .append(" | ")
        .append(zipcode).toString();

    }

    @Override
    public String toString() {
        return new StringBuilder()
        .append("Record = {")
        .append("First Name : ").append(firstName)
        .append(", Last Name : ").append(lastName)
        .append(", Address : ").append(address)
        .append(", City : ").append(city)
        .append(", State : ").append(state)
        .append(", Zipcode : ").append(zipcode)
        .append(" }").toString();
    }
}

RecordSorter.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class RecordSorter {
  
  

    public Map<String,String> sortRecordsFromFileByKeyWithOutputFile(
        String inFileName,
        Comparator<Record> comparator,
        Sorter sorter,
        String outFileName) throws FileNotFoundException, IOException {

        Map<String,String> performanceData = new LinkedHashMap<String,String>();

        File inFile = new File(inFileName);
        File outFile = new File(outFileName);

        BufferedReader br = new BufferedReader(new FileReader(inFile));
        List<Record> unsortedRecords = new ArrayList<Record>();
        List<Record> sortedRecords = null;

        int numberOfRecords = Integer.parseInt(br.readLine());

        String line;
        while((line = br.readLine()) != null) {
            Record r = new Record(line);
            unsortedRecords.add(r);
        }

        try {
            br.close();
        } catch (Exception e) {
            System.out.println("Failed to close buffered reader");
        }

        List<Record> unsortedSublist = new ArrayList<Record>();
      
        for (int i = 0; i < numberOfRecords; ++i) {
            unsortedSublist.add(unsortedRecords.get(i));
            long startTime = System.nanoTime();
            sorter.sortList(unsortedSublist, comparator);
            //unsortedSublist now sorted...
            long endTime = System.nanoTime();
            long totalTime = (endTime - startTime) / 1000; //convert to microseconds
            performanceData.put((i + 1) + "", totalTime + "");
            if ((i + 1) == numberOfRecords) {
                sortedRecords = new ArrayList<Record>(unsortedSublist);
            }
        }

        //write to disk

        if (!outFile.exists()) {
            outFile.createNewFile();
        }

        BufferedWriter bw = new BufferedWriter(new FileWriter(outFile));
        bw.write(String.format("%d%n",numberOfRecords));
        for (Record r : sortedRecords) {
            bw.write(String.format("%s%n", r.toDelimitedString()));
        }

        try {
            bw.close();
        } catch (Exception e) {
            System.out.println("Failed to close buffered writer");
        }


        return performanceData;
    }

  

}

StateComparator.java

import java.util.Comparator;

public class StateComparator implements Comparator<Record> {

    public int compare(Record a, Record b) {
        if (a == null && b == null) {
            return 0;
        } else if ((a == null && b != null)
                || (a != null && b == null)) {
            return a == null ? 1 : -1;
        } else {
            return a.state.compareToIgnoreCase(b.state);
        }
    }
}

FirstNameComparator.java

import java.util.Comparator;

public class FirstNameComparator implements Comparator<Record> {
    public int compare(Record a, Record b) {
        if (a == null && b == null) {
            return 0;
        } else if (
            (a == null && b != null)
            || (a != null && b == null)) {
            return a == null ? 1 : 0;
        } else {
            return a.firstName.compareToIgnoreCase(b.firstName);
        }
    }
}

AddressComparator.java

import java.util.Comparator;

public class AddressComparator implements Comparator<Record> {

    public int compare(Record a, Record b) {
        if (a == null && b == null) {
            return 0;
        } else if ((a == null && b != null)
                || (a != null && b == null)) {
            return a == null ? 1 : -1;
        } else {
            return a.address.compareToIgnoreCase(b.address);
        }
    }
}

Sorter.java

import java.util.Comparator;
import java.util.List;

public interface Sorter<T> {
    void sortList(List<T> records, Comparator<T> comparator);
}


LastNameComparator.java

import java.util.Comparator;

public class LastNameComparator implements Comparator<Record> {
    public int compare(Record a, Record b) {
        if (a == null && b == null) {
            return 0;
        } else if (
            (a == null && b != null)
            || (a != null && b == null)) {
            return a == null ? 1 : 0;
        } else {
            return a.lastName.compareToIgnoreCase(b.lastName);
        }
    }
}

InsertionsortSorter.java

import java.util.Comparator;
import java.util.List;

public class InsertionsortSorter implements Sorter<Record> {

    public void sortList(List<Record> records, Comparator<Record> comparator) {
        int j;
        Record key;
        int i;

        for (j = 1; j < records.size(); j++)
        {
            key = records.get(j);
            for (i = j - 1; (i >= 0) && comparator.compare(key, records.get(i)) < 0; i--)
            {
                records.set(i + 1, records.get(i));
            }

            records.set(i + 1, key);
        }
    }
}

Explanation / Answer

The text file stored through JDBC.

The values in the text file are read through buffer.

The values from the text file i.e name, address,details are append based on the mapping funtion.

the values are stored in the form of list,records and files.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote