So far I have made an array inputted text into notepad so that the file can be saved and a word count can be done also i have made it so it can be in alphabetical order however i am struggling on removing duplicate words from the file such as "i am Kevin and i am amazing"
i want this code to count the words sort them into alphabetical order and remove the duplicate words
the first two are done the third i am struggling on
public class Program3 {
public static void main(String[] args) {
ArrayList<order> ordering = new ArrayList<order>();
String words = readTextFile(filename);
int lineEnd = words.length() - 1;
boolean isWord = false;
String tempWord = "";
String temp = "";
for(int x = 0; x<words.length(); x++){
if(Character.isLetter(words.charAt(x)) && x != lineEnd){
isWord=true;
tempWord = tempWord + words.charAt(x);
}else if (!Character.isLetter(words.charAt(x)) && isWord){
isWord = false;
ordering.add(new order(tempWord));
tempWord="";
}else if(Character.isLetter(words.charAt(x)) && x == lineEnd){
tempWord = tempWord + words.charAt(x);
}
}
for (order o : ordering){
o.display();
}
System.out.println("");
Comparator c = new Comparator<order>(){
@Override
public int compare(order word1, order word2)
{
return word1.ordering().compareTo(word2.ordering());
}
};
ordering.sort(c);
for (order o : ordering){
o.display();
}
//remove duplicates
}
public static String readTextFile(String filename){
String returnValue="";
FileReader file = null;
try {
file = new FileReader(filename);
BufferedReader reader = new BufferedReader(file);
String line = "";
while ((line = reader.readLine()) != null){
returnValue = returnValue + line + "\n";
}
reader.close();
} catch(Exception e){
throw new RuntimeException(e);
}finally{
if (file!=null){
try {
file.close();
} catch (IOException e){
//Ignore issues during closing
}
}
}
return returnValue;
}
}