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

Programming Problems: Unique Words +--------------------------------------+ You

ID: 3674224 • Letter: P

Question

 Programming Problems: Unique Words         +--------------------------------------+          You are to write a Java program that processes an input text file and identifies         the unique words or terms in the file. For instance, given the text in the          "Input Text" below, the result would be as shown in the "Output Text" section.           Input Text:             "I have been a happy man ever since January 1, 1990, when I no longer had              an email address. I had used email since about 1975, and it seems to me that              15 years of email is plenty for one lifetime."          Output Text:             I, have, been, a, happy, man, ever, since, January, 1, 1990, when, no,             longer, had, an, email, address, used, about, 1975, and, it, seems,             to, me, that, 15, years, of, is, plenty, for, one, lifetime.            Requirements         +--------------------------------------+          1. Use the input file "text.txt" provided along with assignment.          2. Use the ArrayList class to compile the unique words/terms.                ArrayList<String> words = new ArrayList<String>();          3. The output should be the unique words separated by commas.          4. The source code must be formatted and documented. 

Explanation / Answer

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class MyDistinctFileWords {

public List<String> getDistinctWordList(String fileName){

FileInputStream fis = null;
DataInputStream dis = null;
BufferedReader br = null;
ArrayList<String> wordList = new ArrayList<String>();
try {
fis = new FileInputStream(fileName);
dis = new DataInputStream(fis);
br = new BufferedReader(new InputStreamReader(dis));
String line = null;
while((line = br.readLine()) != null){
StringTokenizer st = new StringTokenizer(line, " ,.;:"");
while(st.hasMoreTokens()){
String tmp = st.nextToken().toLowerCase();
if(!wordList.contains(tmp)){
wordList.add(tmp);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try{if(br != null) br.close();}catch(Exception ex){}
}
return wordList;
}

public static void main(String a[]){

MyDistinctFileWords distFw = new MyDistinctFileWords();
List<String> wordList = distFw.getDistinctWordList("F:/Text.txt");
for(String str:wordList){
System.out.println(str+",");
}
}
}