2013-03-04 64 views
-2

好的,所以這個代碼的標準是如果在字符串2中找到90%的字符串中找到的單詞,並且如果在字符串1中找到了字符串2中的90%的時間,那麼布爾值應該是真的,應該說抄襲已經發生。代碼如下;剽竊探測器問題

import java.io.*; 
import java.util.Scanner; 

public class PlagiarismDetector 
{ 
    public static void main(String[] args) { 
Scanner reader = new Scanner(System.in); 
System.out.println("What file is the first file?"); 
String fileOne = reader.next(); 

String stringOne = readStringFromFile(fileOne); 

System.out.println("What file is the second file?"); 
String fileTwo = reader.next(); 
String stringTwo = readStringFromFile(fileTwo); 

if (stringOne == null || stringTwo == null) 
{ 
    return; 
} 

System.out.println("Comparing the 2 files......"); 
System.out.println("The result of the 2 files is ...."); 

if (compareStrings(stringOne, stringTwo)) 
{ 
    System.out.println("Plagiarism detected. Cheaters!!!!"); 
} 
else 
{ 
     System.out.println("No plagiarism detected"); 
     } 
    } 

    public static String readStringFromFile(String filename) 
    { 
String builder = ""; 
try 
{ 
    Scanner fileReader = new Scanner(new File(filename)); 
    while (fileReader.hasNextLine()) 
    { 
    builder = builder + fileReader.nextLine() + "\n"; 
    } 

    return builder; 
} 
catch (Exception e) 
{ 
    System.out.println("An error occurred while trying to open the file " + filename + ". Is the file located inside the same folder as the .class file and with the identical name?"); 
    return null; 
} 
    } 

    public static boolean compareStrings (String a, String b) 
{ 
    boolean checkForPlagiarism = true; 
    String[] piecesA = a.split("\\s"); 
    String[] piecesB = b.split("\\s"); 

    double count1 = 0.0; 
    double count2 = 0.0; 
    for (int counter = 0; counter < piecesA.length; counter++) 
    { 
     for(int counter2 = 0; counter < piecesB.length; counter2++) 
     { 
      if(piecesA[counter].equals(piecesB[counter2])) 
      { 
      count1++; 
      } 
     } 
    } 
    for (int counter = 0; counter < piecesB.length; counter++) 
    { 
     for(int counter2 = 0; counter < piecesA.length; counter2++) 
     { 
      if(piecesA[counter2].equals(piecesB[counter])) 
      { 
      count2++; 
      } 
     } 
    } 

    if((count1/piecesA.length) >= 0.9 && (count2/piecesB.length) >= 0.9) 
    { 
    checkForPlagiarism = true; 
    }  
    return checkForPlagiarism; 
    } 
} 

我收到這個錯誤消息,但是我還沒有能夠調試;

java.lang.ArrayIndexOutOfBoundsException: 3 
at PlagiarismDetector.compareStrings(PlagiarismDetector.java:67) 
at PlagiarismDetector.main(PlagiarismDetector.java:24) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272) 

任何幫助將深深感謝球員。

+4

這是一個不同的問題比你早發佈的一個? http://stackoverflow.com/questions/15194877/plagiarism-detector。如果是這樣,爲什麼不能從這篇文章中學習? – rajesh 2013-03-04 06:05:55

+0

似乎是相同的代碼。 @OP不會創建新帳戶來發布新問題。 – 2013-03-04 06:07:00

+0

不*完全相同。 for循環中的結束條件不同(正如在另一個問題的答案中所建議的那樣)。 – user000001 2013-03-04 06:08:15

回答

2

for(int counter2 = 0; counter < piecesB.length; counter2++)

大概應該是

for(int counter2 = 0; counter2 < piecesB.length; counter2++)