Array Duplicate Comparison

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 628 Views - Last Post: 06 February 2012 - 12:14 PM Rate Topic: -----

Topic Sponsor:

#1 Examiner12  Icon User is online

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 77
  • Joined: 02-February 12

Array Duplicate Comparison

Posted 02 February 2012 - 04:32 PM

Good day. This is my first post, so please bare with me.

I've searched and found suggestions, but hit a wall with comparing a List of Files from one directory to another. I want to print the files in the second directory that are also contained in the first. Here's what I have so far:

import java.io.File;

public class diffcheck2
{
public static void main(String[] args)
{
//populate the arrays
	File f = new File("C:/DIFFCHECK/DIR1/");                
	File[] rec = f.listFiles();  
	
	File g = new File("C:/DIFFCHECK/DIR2/");                
	File[] hist = g.listFiles();

//loop through the contents of rec
//and comparing each element to each element in hist
//boolean foundSwitch = false;

	//outer loop for all the elements in rec[i]
	for(int i = 0; i < rec.length; i++)
	{
	//inner loop for all the elements in hist[j]
	for (int j = 0; j < hist.length;j++)
	{
	//compare rec to hist and output results
	if( rec[i].equals(hist[j]))
	{
	//foundSwitch = true;
	System.out.println( "Received File" + rec[i] + " was found in Trans Hist" );
	}
	else {
		System.out.println("no dups");
	}
	}


	}
	}
	}



C:/DIFFCHECK/DIR1/ contains: TEST_DUP.txt, TEST_UNI.txt

C:/DIFFCHECK/DIR2/ contains: TEST2.txt, TEST_DUP.txt

I'm looking for it to print: TEST_DUP.txt

Here's the output I receive:

no dups
no dups
no dups
no dups

I'm not sure what I'm doing wrong.

Is This A Good Question/Topic? 0
  • +

Replies To: Array Duplicate Comparison

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,072
  • Joined: 05-April 11

Re: Array Duplicate Comparison

Posted 02 February 2012 - 04:39 PM

You are testing if two instances are the same (which they will never be in your example)

Instead you could use the method getName() to get the file name.

if (rec[i].isFile() && hist[j].isFile())
  if (rec[i].getName().equals(hist[j]).getName()) {
    ...
  }


This post has been edited by CasiOo: 02 February 2012 - 04:39 PM

Was This Post Helpful? 2
  • +
  • -

#3 Examiner12  Icon User is online

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 77
  • Joined: 02-February 12

Re: Array Duplicate Comparison

Posted 03 February 2012 - 08:39 AM

View PostCasiOo, on 02 February 2012 - 04:39 PM, said:

You are testing if two instances are the same (which they will never be in your example)

Instead you could use the method getName() to get the file name.

if (rec[i].isFile() && hist[j].isFile())
  if (rec[i].getName().equals(hist[j]).getName()) {
    ...
  }



I very much appreciate your input CasiOo. Unfortunately, I receive the error as follows:

Cannot invoke getName() on the primitive type boolean.

I modified the 'if' portion as follows, using your input:

		if (rec[i].getName() == hist[j].getName()) {  
System.out.println( rec[i] + "file match");
		} 
		else {System.out.println("no matches");
}



I receive 'no matches' as Output! I can asure you there are two IDENTICALLY named files in the paths:

C:/DIFFCHECK/DIR1/TEST_DUP.txt
C:/DIFFCHECK/DIR2/TEST_DUP.txt

I know this is simple, why can't I wrap my head around this?!
Was This Post Helpful? 0
  • +
  • -

#4 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 106
  • View blog
  • Posts: 359
  • Joined: 28-November 09

Re: Array Duplicate Comparison

Posted 03 February 2012 - 08:45 AM

Never use == for comparing Strings, because it will compare memory locations, not the actual string. CasiOo just put his .getName() a little off. The code below should work.

if (rec[i].isFile() && hist[j].isFile())
  if (rec[i].getName().equals(hist[j].getName())) {
    ...
  }



This post has been edited by ianian112: 03 February 2012 - 08:50 AM

Was This Post Helpful? 2
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1778
  • View blog
  • Posts: 3,357
  • Joined: 19-March 11

Re: Array Duplicate Comparison

Posted 03 February 2012 - 08:47 AM

Simple typo, a paren out of place. Try it this way:

if (rec[i].getName().equals(hist[j].getName()))



Not to get cranky about this, but this is the sort of thing you should try to spot for yourself! If you see "cannot call getName() on a boolean", you can easily look to see where that would be coming from.
Was This Post Helpful? 1
  • +
  • -

#6 bhandari  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 753
  • Joined: 31-January 08

Re: Array Duplicate Comparison

Posted 03 February 2012 - 09:22 AM

I recently posted a tutorial on Duplicate elements in Array

This post has been edited by bhandari: 03 February 2012 - 09:22 AM

Was This Post Helpful? 0
  • +
  • -

#7 Examiner12  Icon User is online

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 77
  • Joined: 02-February 12

Re: Array Duplicate Comparison

Posted 03 February 2012 - 09:28 AM

Apoligies, Jon. I'll take more care in the future. Please bare with me here--

The issue I have with using 'if' vice 'for' as indicated above is that I have to initialize the int's i and j. If I initialize to 0, how will the code loop through checking each file? Seems like this is just comparing the first element in the arrays, not all of array rec to all of array hist.

Running as follows, I get no output:

import java.io.File;

public class diffcheck2
{
public static void main(String[] args)
{
//populate the arrays
	File f = new File("C:/DIFFCHECK/DIR1/");                
	File[] rec = f.listFiles();  
	
	File g = new File("C:/DIFFCHECK/DIR2/");                
	File[] hist = g.listFiles();
	
		int i = 0;
		int j = 0;
		if (rec[i].isFile() && hist[j].isFile())
						
			if (rec[i].getName().equals(hist[j].getName()))  {  

			 System.out.println(rec[i].getName());

			} 
		}

}


Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1778
  • View blog
  • Posts: 3,357
  • Joined: 19-March 11

Re: Array Duplicate Comparison

Posted 03 February 2012 - 09:46 AM

Quote

Apoligies, Jon. I'll take more care in the future. Please bare with me here--


No bother. Just a word to the wise, it's good to get in the habit of asking yourself, "how'd that happen?".

For your current question, in this case what will happen is this:

if rec[0] is a plain vanilla file (and not a directory) and hist[j] is a plain vanilla file, and the two files have the same name, then you'll see that name printed to the console.


That's it.

Now, in most systems, the first two files in any directory listing are "." and ".." - the current directory, and the parent directory. Those are not plain vanilla files (they are directories) so you wouldn't enter the first if's statement block.

You could test this by adding an else clause, something like this:

	  if (rec[i].isFile() && hist[j].isFile())
            if (rec[i].getName().equals(hist[j].getName()))  { 
             System.out.println(rec[i].getName());
            }
            else
            { 
                System.out.println("names were not equal, but both Files were files");
            } 
        }
        else 
        {
           System.out.println("one of the files isn't a file!"
           System.out.println("rec[i] = " + rec[i].getName() + 
                 ". is a file? " + rec[i].isFile());
           System.out.println("hist[j] =  " + hist[j].getName() +
                 ". is a file? " +rec[i].isFile());
        }





(this is not tested, so please forgive me if I've fatfingered the complex printlns...)
Was This Post Helpful? 1
  • +
  • -

#9 Examiner12  Icon User is online

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 77
  • Joined: 02-February 12

Re: Array Duplicate Comparison

Posted 03 February 2012 - 10:51 AM

I want to thank everyone that posted for their patience and timely assistance.

I believe I have found my solution. Hope this is helpful to other 'noobs' like me!


import java.io.File;

public class diffcheck2
{
public static void main(String[] args)
{
//populate the arrays
	File f = new File("C:/DIFFCHECK/DIR1/");                
	File[] rec = f.listFiles();  
	
	File g = new File("C:/DIFFCHECK/DIR2/");                
	File[] hist = g.listFiles();
	
	for(int i=0;i<rec.length;i++)
	{    boolean found = false;    
	for(int j=0;j<hist.length;j++){       
		if((rec[i].getName().equals(hist[j].getName()))){          
			found = true;          
			break;       }    }     
	if (found) 
	{ 
	System.out.println("DUPLICATE: "+ rec[i].getName());   
	} 
	
	} 
}

}



Was This Post Helpful? 2
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1778
  • View blog
  • Posts: 3,357
  • Joined: 19-March 11

Re: Array Duplicate Comparison

Posted 03 February 2012 - 11:38 AM

Looks good to me.
Was This Post Helpful? 0
  • +
  • -

#11 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,072
  • Joined: 05-April 11

Re: Array Duplicate Comparison

Posted 03 February 2012 - 11:52 AM

Well you are not only checking if files are duplicate, but also directories
Was This Post Helpful? 1
  • +
  • -

#12 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 106
  • View blog
  • Posts: 359
  • Joined: 28-November 09

Re: Array Duplicate Comparison

Posted 03 February 2012 - 11:55 AM

View PostCasiOo, on 03 February 2012 - 11:52 AM, said:

Well you are not only checking if files are duplicate, but also directories

because he is calling .getName() he is only checking if the file names are the same. getAbsolutePath() or getCanonicalPath() would compare the directories as well

I get what your saying, yea he removed the .isFile() check.

This post has been edited by ianian112: 03 February 2012 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

#13 Examiner12  Icon User is online

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 77
  • Joined: 02-February 12

Re: Array Duplicate Comparison

Posted 03 February 2012 - 03:53 PM

Ok guys, I'm satisfied with the basic function, but I want to add an enhancement.

The file name check duplication is working fine. However, to check if the file is a true duplicate, I want to compare their sizes.

I have no problem accessing the file in DIR1 stored in rec[i]... BUT I can't get it to see file in DIR2 stored in hist[j] !! Eclipse says I need to initialize variable j, but it's there plain as i. I've played with the structure tons and can't get it!

I've commented out the portion thats not working. What gives!?

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

public class diffcheck2
{
public static void main(String[] args) throws IOException
{
//populate the arrays
	File f = new File("C:/DIFFCHECK/DIR1/");                
	File[] rec = f.listFiles();  
	
	File g = new File("C:/DIFFCHECK/DIR2/");                
	File[] hist = g.listFiles();
	
	for(int i=0;i<rec.length;i++){
	    boolean found = false;    
	for(int j=0;j<hist.length;j++){       
		if((rec[i].getName().equals(hist[j].getName()))){  
			found = true;          
			break;       }    }     
	if (found) 
	{ 
				System.out.println("*Duplicate File: "+ 
					rec[i].getName() + " - " +rec[i] + " - " + rec[i].length() 
					//+hist[j].getName() + " - " + hist[j] + hist[j].length() //***why can't it see hist[j]?? 
						);	

		}
	}
	
	} 
	
}




Was This Post Helpful? 0
  • +
  • -

#14 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,072
  • Joined: 05-April 11

Re: Array Duplicate Comparison

Posted 04 February 2012 - 07:00 PM

Please keep the same syntax all the way through your code, and indent it the right way, it will help you so much!

I formatted your code, and it is now clear that the for-loop with the int j is not in the scope

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

public class diffcheck2 {
	public static void main(String[] args) throws IOException {
	//populate the arrays
		File f = new File("C:/DIFFCHECK/DIR1/");                
		File[] rec = f.listFiles();  
		
		File g = new File("C:/DIFFCHECK/DIR2/");                
		File[] hist = g.listFiles();
		
		for(int i=0;i<rec.length;i++) {
			boolean found = false;  
			
			for(int j=0;j<hist.length;j++) {       
				if((rec[i].getName().equals(hist[j].getName()))) {  
					found = true;          
					break;       
				}    
			}     
			if (found) 
				System.out.println("*Duplicate File: "+ 
				rec[i].getName() + " - " +rec[i] + " - " + rec[i].length() 
				//+hist[j].getName() + " - " + hist[j] + hist[j].length() //***why can't it see hist[j]?? 
				);
		}
	} 
}


Was This Post Helpful? 1
  • +
  • -

#15 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7519
  • View blog
  • Posts: 28,887
  • Joined: 27-December 08

Re: Array Duplicate Comparison

Posted 04 February 2012 - 08:15 PM

View Postbhandari, on 03 February 2012 - 11:22 AM, said:

I recently posted a tutorial on Duplicate elements in Array

Sets are great for eliminating duplicates. Remember though that Java Collections are generic, and generics should be used so as to avoid a deprecation warning.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2