0

This is part of my code which I am doing under my college project so basically I am making a simple plagiarism detection using two string matching algorithms and using it in the main class and for I did some mistakes in loops so because of that my output is repeating 12 times and checked my code again and again but can't really figure out where I went wrong I really need someone to help me with this I have to submit this by end of this month I am attaching photo of my output Output

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;


public class MClass {

    
    public static void main(String[] args) throws IOException {

        String ptrnLine, textLine,inpLine,sFilePath,srcLine;
        int srcLineIndex=1, inpLineIndex=1;
        KMP kmpComponent;
        RabinKarp rkComponent;
        int inputLen,srcLen,patterntextLength;
        double  kmpSimRatio = 0;  
        int rkNumberOfMatches;
        int fullTextLength=0, fullPatternLength=0;

        boolean rkPlagarismStatus = false;
        final File folder = new File("D:\\Project");
        File fileKmp = new File("kmp.txt"); 
        File fileRK = new File("rk.txt"); 
        int coun = 0;
        
        fileKmp.delete();
        fileRK.delete();
        
        FileWriter outKmpFile = new FileWriter("kmp.txt", true);
        FileWriter outRkFile = new FileWriter("rk.txt", true);
        
        
        for (final File fileEntry : folder.listFiles()) {
            sFilePath = fileEntry.getPath();
            srcLineIndex=1;
            File sourceFile = new File("source.txt");
            File inputFile = new File( "input.txt"); 
            
            @SuppressWarnings("resource")
            BufferedReader sReader = new BufferedReader( new FileReader(sourceFile));
            while((srcLine = sReader.readLine())!=null)
            {
                
                BufferedReader reader = new BufferedReader( new FileReader(inputFile));
                inpLineIndex=1;
                fullTextLength = fullTextLength+srcLine.length();
                while((inpLine = reader.readLine())!=null)
                {
                    inputLen = inpLine.length();
                    srcLen = srcLine.length();
                
                    if(inputLen>0 && srcLen>0) 
                    {
                        if(srcLen>inputLen)
                        {   
                        textLine = srcLine;
                        ptrnLine = inpLine;
                        }
                        else
                        {  
                        textLine = inpLine;
                        ptrnLine = srcLine;
                        }

                        patterntextLength = ptrnLine.length();
                        
                        
                        if(coun<1)
                        {
                        fullPatternLength = fullPatternLength+ ptrnLine.length();
                        }
                        
                    // KMP Algorithm
                        kmpComponent = new KMP();
                         if(patterntextLength!=0)
                        { kmpSimRatio= (kmpComponent.searchSubString(textLine, ptrnLine)/(double)(patterntextLength));
                        }
                        System.out.println("KMP Algorithm Result");
                        System.out.println("Similarity ratio = "+kmpSimRatio*100.000+" Line Number of the input file= "+inpLineIndex+
                                " Line Number of the source file  = "+srcLineIndex);
                        System.out.println("------------------------------------------------------------------------------------------------------------------------------------------");
                        PrintWriter outPKmpFile = new PrintWriter(outKmpFile);
                
                        if(kmpSimRatio>0.60)
                        { outPKmpFile.append("Line "+inpLineIndex + " of the input file has plagarised " +kmpSimRatio*100.000+
                                "% from line "+srcLineIndex +" of the source file \n");
                        
                        }

   

                                                
                        //Rabin Karp Algorithm
                        rkComponent = new RabinKarp();
                        if(patterntextLength!=0)
                        {
                            rkNumberOfMatches = rkComponent.search(ptrnLine,textLine);
                            if(rkNumberOfMatches>0)
                            {  
                                rkPlagarismStatus = true;
                            }
                            else
                            {
                                rkPlagarismStatus =false;
                            }
                            if(rkPlagarismStatus)
                            {   System.out.println("Rabin Karp Algorithm Result");
                                System.out.println(" Line Number of the input file= "+inpLineIndex+ " is plagarised from" +
                                    " Line Number of the source file = "+srcLineIndex+" Number of times string matched was "+rkNumberOfMatches);
                                System.out.println("------------------------------------------------------------------------------------------------------------------------------------------");
                             PrintWriter outPRkFile = new PrintWriter(outRkFile);
                             outPRkFile.append("Line "+inpLineIndex + " of the input file has plagarised from line "+srcLineIndex +" of the source file "+fileEntry.getName()+
                                     " "+rkNumberOfMatches+" time string matching found\n");
                        
                            }
                            
                        }
                        inpLineIndex++;
                    }
                    
                }
                coun++;
                srcLineIndex++;
            }
            
        }
        
        outKmpFile.close();
        outRkFile.close();
        

        
    }

}


py8141
  • 1
  • 3
  • 1
    `for (final File fileEntry : folder.listFiles())` isn't it a for loop? Look like you need to change the src file in every iteration, like : `File sourceFile = new File(sFilePath);` – Jean-Baptiste Yunès Jul 24 '21 at 10:04
  • Simplify the loop first. Remove all the actual checking stuff and make sure the loop is working properly. – Amir M Jul 24 '21 at 10:08
  • In ```while ((inpLine = reader.readLine()) != null)```, where's that ```reader``` declared? There are a lot of missing delacrations, can you show them? – Thomas Herondale Jul 24 '21 at 10:57
  • I've copied my full code if there's some confusion please look once again @ThomasHerondale – py8141 Jul 24 '21 at 11:25
  • Your code is full of problems, such as: if you write ```if(inputLen>0 && srcLen>0)``` and then assign ```patterntextLength``` either to ```inputLen``` or to```srcLen```, your ```if(patterntextLength!=0)``` is always true – Thomas Herondale Jul 24 '21 at 13:18

0 Answers0