Type your question here Write a java program that will read a sequence of names
ID: 3552626 • Letter: T
Question
Type your question here
Write a java program that will read a sequence of names (first name followed by last name, separated by at least one space) from a text file and will 1) remove all duplicate names and 2) write the names (last name followed by a comma, followed by one space, followed by firstname in ascending order to a textfile. For example, if the input file contains: Colette Sapienza Gretta Beumer Emmanuel Mossman Colette Sapienza Shawanda Hutt Adriana Montilla Adriana Montilla Adriana Montilla. MaricaStanfill Neva Shover Tijuana Means Adriana Montilla Gerri Koenig Kirsten Beres Kirsten Beres The output file must contain: Beres, Kirsten Beumer, Gretta Hutt, Shawanda Koenig, Gerr Means, Tijuana. Montilla, Adriana Mossman, Emmanuel Sapienza, Colette Shover, Neva. Stanfill, Marica Your program must contain and use the following methods:Explanation / Answer
Input File used:
"input.txt":
Colette Sapienza
Gretta Beumer
Emmanuel Mossman
Colette Sapienza
Shawanda Hutt
Adriana Montilla
Adriana Montilla
Adriana Montilla
Marica Stanfill
Neva Shover
Tijuana Means
Adriana Montilla
Gerri Koenig
Kirsten Beres
Kirsten Beres
------------------------------------------------------------------------------------------------------------------------------
Code:
--------------------------------------------------------
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.Writer;
import java.util.*;
public class Sortnames {
public static ArrayList<String> sortedWordList = new ArrayList<String>();
public static int find(ArrayList<String> names, int i){
String s = names.get(i);
for(int j=i+1; j<names.size(); j++){
if(s.equals(names.get(j))){
return j;
}
}
return -1;
}
public static void removeDuplicates(ArrayList<String> names){
for(int i=0; i<names.size(); i++){
int index = find(names, i);
if(index != -1){
names.remove(index);
i--;
}
}
}
public static void sort(ArrayList<String> names){
String first="",last="";
for(int i=0; i<names.size(); i++){
String str = names.get(i);
StringTokenizer st = new StringTokenizer(str);
int count = 0;
while (st.hasMoreElements()) {
if(count == 0){
first = (String)st.nextElement();
}
else {
last = (String)st.nextElement();
}
count ++;
}
StringBuilder sentence = new StringBuilder();
sentence.append(last);
sentence.append(", ");
sentence.append(first);
String newStr = sentence.toString();
sortedWordList.add(newStr);
}
// now sort the modified names
for( int k=0; k<sortedWordList.size()-1; k++ ) {
for ( int j=k+1; j<sortedWordList.size(); j++ ){
if( sortedWordList.get(j).compareTo(sortedWordList.get(k)) < 0) {
String temp = sortedWordList.get(k);
sortedWordList.set(k,sortedWordList.get(j));
sortedWordList.set(j,temp);
}
}
}
}
public static void main(String args[]) {
try {
ArrayList storeWordList = new ArrayList();
int i;
char c;
String inputfile = "input.txt";
String outputfile = "sortedoutput.txt";
Scanner scan = new Scanner(System.in);
System.out.println("Enter the name of input file:");
inputfile = scan.next();
System.out.println("Enter the name of output file:");
outputfile = scan.next();
StringBuffer strBuff=null;
Writer output = null;
File file =null;
Writer sortedoutput = null;
boolean select =false;
//*********************************************** Reading data from Input file
FileInputStream fstream = new FileInputStream(inputfile);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
storeWordList.add(strLine);
}
//Close the input stream
in.close();
// removing duplicates
removeDuplicates(storeWordList);
// sorting arrayList
sort(storeWordList);
//===================================================
// write output to the file "outputfile"
File sortedfile = new File(outputfile);
sortedoutput = new BufferedWriter(new FileWriter(sortedfile));
for(int m=0; m<sortedWordList.size(); m++)
{
sortedoutput.write(sortedWordList.get(m));
sortedoutput.write(" ");
}
sortedoutput.close();
System.out.println("Names Sorted SUCCESFULLY");
br.close();
in.close();
fstream.close();
} catch (Exception e) {
//Catch exception if any
System.out.println("Error: " + e);
}
}
}
-----------------------------------------------------------------------------------------------------------------------
Ouptut File:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.