循环中存在一些问题,如果可能,它会重复 12 次帮助

问题描述

这是我在大学项目下做的代码的一部分,所以基本上我正在使用两个字符串匹配算法进行简单的抄袭检测,并在主类中使用它,因为我在循环中犯了一些错误,因此我的输出重复了 12 次,并一次又一次地检查我的代码,但无法真正弄清楚我哪里出错了我真的需要有人帮我解决这个问题我必须在本月底提交这个我附上我的输出照片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();
        

        
    }

}


解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)