Home > database >  How can we solve this error in comparing two folders and storing those files with same name in an ar
How can we solve this error in comparing two folders and storing those files with same name in an ar

Time:12-12

I am a beginner in Java and I wanted to create a program that compares two folders and can store the files with same name in an array. I want to access this array in another class to display the name of files.



    public int fileCompare(String path1, String path2){
        int i,j,k=0;
        flag=0;
        File file1 = new File(path1);
        File file2 = new File(path2);     
        File[] array_file1=file1.listFiles();    
        File[] array_file2=file2.listFiles();
        int size1 = array_file1.length>array_file2.length ?    array_file1.length:array_file2.length;     
        equalFiles1 = new File[size1];
        equalFiles2 = new File[size1];
   
      for(i=0;i<array_file1.length;i  ){
        String n1=array_file1[i].getName();
        for(j=0;j<array_file2.length;j  ){
            String n2=array_file2[j].getName();

            if (n1.equals(n2)) {
                flag  ;
                equalFiles1[k]= array_file1[i] ;
                equalFiles2[k] = array_file2[j];
                k  ;
            } 
        }
      }



The above code contains the method for comparing two folders and returning the arrays. This array is then accessed using the below code.

    for (File file : c.equalFiles1 ) { // c is the object of previous class
        for(int i=0; i < c.flag;i  ){
           row1[i]= file.getName() ;                                                                             
        }

    }
   

But the error was shown as

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
        

CodePudding user response:

the problem is in the second loop

for(j=0;j<array_file2.length;j  ){
        String n2=array_file2[i].getName();

you put i instead of j hence when file1 length is greater than file2, the index got out of bound

and in demo2hack you init string row using c.flag while c.flag is still 0

you should initialized your string row after calling c.compare file if you wanna use c.flag

CodePudding user response:

The code below compiles and prints the elements of array row1. You can copy and paste in https://www.onlinegdb.com/online_c_compiler and select JAVA on the pulldown box, top right of screen. You still need to fix the logic to get meet the requirements

import java.io.*;
public class Main
{
     public static final String DIR_PATH1 = "./";     // replace with dir path
     public static final String DIR_PATH2 = "../../";
     
    public static void main(String[] args) {
        
        
        DemoHack1 c = new DemoHack1();
        String folderpath1 = DIR_PATH1;
        String folderpath2 = DIR_PATH1;
        
    
        c.flag = c.fileCompare(folderpath1, folderpath2);
        if (c.flag > 0) {  // make sure to void creating an array of size zero
            String[] row1= new String[c.flag];
            
            for (File file : c.equalFiles1 ) {
                for(int i=0; i < c.flag;i  ){
                   row1[i]= file.getName() ;  
                   
                }
    
            }
          
            for(String r : row1){
                System.out.println("files in row>> "  r )  ;  
                   
            }
        }
        
        System.out.println("-----end -----");
         
    }
    
}   
class DemoHack1 {
    
        public File[] equalFiles1;
        public File[] equalFiles2;
        
        int flag;
        public int fileCompare(String path1, String path2) {
            int k=0;
            flag=0;
            File file1 = new File(path1);
            File file2 = new File(path2);     
            File[] array_file1=file1.listFiles();    
            File[] array_file2=file2.listFiles();
            int size1 = array_file1.length>array_file2.length ?    array_file1.length:array_file2.length;     
            equalFiles1 = new File[size1];
            equalFiles2 = new File[size1];
       
          for(int i=0; i< array_file1.length; i  ){
            String n1=array_file1[i].getName();
            //System.out.println("n1>> "   n1);         // use print stmt to check the values
            
            for(int j=0; j <array_file2.length; j  ){
                String n2=array_file2[j].getName();
                //System.out.println("n2>> "   n2);
                
                if (n1.equals(n2)) {
                    flag  ;
                    equalFiles1[k]= array_file1[i] ;
                    equalFiles2[k] = array_file2[j];
                    k  ;
                } 
            }
          }
     
         
          return flag;
        }
   }

Output:
 files in row>> DemoHack1.class
files in row>> DemoHack1.class
files in row>> DemoHack1.class
files in row>> DemoHack1.class
files in row>> DemoHack1.class
-----end -----
  • Related