0

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;
    }
    }
Stultuske
  • 9,296
  • 1
  • 25
  • 37
danny
  • 1
  • 5
    You can use Set to remove duplicates instead of List – Harshit May 16 '18 at 10:10
  • Please use set... – Amit May 16 '18 at 10:11
  • And make sure `order.hashcode` and `order.equals` are implemented before you tried to use a `Set` like an `HashSet`. – AxelH May 16 '18 at 10:19
  • 1
    When using set, you'll need to count the double words when reading them, though. for future reference: this question is not eclipse related, so tagging it as such, might lead to people refusing to answer. – Stultuske May 16 '18 at 10:25
  • Possible duplicate of [Count the number of Occurrences of a Word in a String](https://stackoverflow.com/questions/22566503/count-the-number-of-occurrences-of-a-word-in-a-string) – AxelH May 16 '18 at 10:55

0 Answers0