2013-03-04 5 views
0

코드를 작성하려고하는 프로그램에 문제가 있습니다. 기본적으로 두 파일에서 읽고 배열로 변환하고 유사점을 비교합니다. 그것은 지금까지 때를 작동했지만 한 첫 .txt 인 파일은 "abcdefghijaaaaaa"를 읽고 두 번째가 .txt 파일은 "abcdefghik"읽을 때, 나는 파일 이름을 입력 한 후 다음과 같은 오류가 발생합니다 :표절 감지기

java.lang.ArrayIndexOutOfBoundsException: 10 
    at PlagiarismDetector.compareStrings(PlagiarismDetector.java:77) 
    at PlagiarismDetector.main(PlagiarismDetector.java:25) 
    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) 

내 코드입니다 다음과 같이 도울 수있는 사람에게 감사 =)

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

    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) 
     {enter code here 
    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"); 

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

     if((count1/(int)piecesA.length)*100 >= 90 && (count2/(int)piecesB.length)*100 >= 90) 
     { 
     checkForPlagiarism = false; 
     }  
     return checkForPlagiarism; 
     } 
    } 

답변

1

이 suspitius 같습니다

: 잘못된 배열의 길이에 의해 제한

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

카운터가

당신은 아마이 작업을 수행 할 수

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

또한 규칙은 다음과 같은 루프를 작성하는 것입니다.

for(int counter2 = 0; counter2 < piecesB.length; counter2++) 
2
  if(piecesA[counter].equals(piecesB[counter])) 

되어야

  if(piecesA[counter2].equals(piecesB[counter]))