Question

Hi all I wrote a mergesort program for a string array that reads in .txt files from the user. But what I want to do now is compare both files and print out the words in file one and not in file two for example apple is in file 1 but not file 2. I tried storing it in a string array again and then printing that out at the end but I just cant seem to implement it. Here is what I have,

FileIO reader = new FileIO();
     String words[] = reader.load("C:\\list1.txt");
     String list[] = reader.load("C:\\list2.txt");

     mergeSort(words);
     mergeSort(list);
     String x = null ;

     for(int i = 0; i<words.length; i++)
     {
         for(int j = 0; j<list.length; j++)
         {
                 if(!words[i].equals(list[j]))
                 {
                      x = words[i];
                 }
         }
     }

     System.out.println(x);

Any help or suggestions would be appriciated!

Was it helpful?

Solution

If you want to check the words that are in the first array but do not exist in the second, you can do like this:

 boolean notEqual = true;           
 for(int i = 0; i<words.length; i++)
     {
         for(int j = 0; j<list.length && notEqual; j++)
         {
                 if(words[i].equals(list[j]))     // If the word of file one exist
                 {                                // file two we set notEqual to false
                      notEqual = false;           // and we terminate the inner cycle
                 }
         }
         if(notEqual)                      // If the notEqual remained  true
           System.out.println(words[i]);   // we print the the element of file one
                                           // that do not exist in the second file

         notEqual = true;                  // set variable to true to be used check
     }                                     // the other words of file one.

Basically, you take a word from the first file (string from the array) and check if there is a word in file two that is equal. If you find it, you set the control variable notEqual to false, thus getting out of the inner loop for and not print the word. Otherwise, if there is not any word on file two that match the word from file one, the control variable notEqual will be true. Hence, print the element outside the inner loop for.

You can replace the printing statement, for another one that store the unique word in an extra array, if you wish.

Another solution, although slower that the first one:

     List <String> file1Words = Arrays.asList(words);
     List <String> file2Words = Arrays.asList(list);

     for(String s : file1Words)
        if(!file2Words.contains(s))
          System.out.println(s);

You convert your arrays to a List using the method Arrays.asList, and use the method contains to verify if the word of the first file is on the second file.

OTHER TIPS

Why not just convert the Arrays to Sets? Then you can simply do result = wordsSet.removeAll(listSet);

your result will contain all the words that do not exist in list2.txt

Also keep in mind that the set will remove duplicates ;)

you can also just go through the loop and add it when you reached list.length-1. and if it matches you can break the whole stuff

FileIO reader = new FileIO();
String words[] = reader.load("C:\\list1.txt");
String list[] = reader.load("C:\\list2.txt");

mergeSort(words);
mergeSort(list);
//never ever null
String x = "" ;

for(int i = 0; i<words.length; i++)
{
    for(int j = 0; j<list.length; j++)
    {
            if(words[i].equals(list[j]))
                 break;
            if(j == list.length-1)
                 x += words[i] + " ";
    }
}

System.out.println(x);

Here is a version (though it does not use sorting)

    String[] file1 = {"word1", "word2", "word3", "word4"};
    String[] file2 = {"word2", "word3"};
    List<String> l1 = new ArrayList(Arrays.asList(file1));
    List<String> l2 = Arrays.asList(file2);
    l1.removeAll(l2);
    System.out.println("Not in file2 " + l1);

it prints

Not in file2 [word1, word4]

This looks kind of close. What you're doing is for every string in words, you're comparing it to every word in list, so if you have even one string in list that's not in words, x is getting set.

What I'd suggest is changing if(!words[i].equals(list[j])) to if(words[i].equals(list[j])). So now you know that the string in words appears in list, so you don't need to display it. if you completely cycle through list without seeing the word, then you know you need to explain it. So something like this:

 for(int i = 0; i<words.length; i++)
 {
     boolean wordFoundInList = false;

     for(int j = 0; j<list.length; j++)
     {
             if(words[i].equals(list[j]))
             {
                  wordFoundInList = true;
                  break;
             }
     }

     if (!wordFoundInList) {
         System.out.println(x);
     }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top