Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8981953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:27:48+00:00 2026-06-15T20:27:48+00:00

Hi all I wrote a mergesort program for a string array that reads in

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:27:49+00:00Added an answer on June 15, 2026 at 8:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

before this I wrote all jquery-code into main fail. Now I want to move
i wrote a regular merge sort array code and all i want to do
I wrote a function that selects all the users from my database and all
I wrote simple code for deleting all entries from android calendar,but it didn't delete
I wrote a script that collects all our log files to one tar file.
all. I wrote an SSIS package that queries data from defined databases and puts
I wrote a tiny Bash script to find all the Mercurial changesets (starting from
First of all, I wrote a simple php page, that picks up some variables
I have done all that from This I have also made .ts and m3u8
Hi All I wrote a static library (libA) that uses another library (libB). How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.