2016-01-07 69 views
0

所以我测试了这段代码,并且当我读取一个文件时,打印时的输出停止并且我得到了java.lang.ArrayIndexOutOfBoundsException: -1 at WordLengths.countWordLengthsWithIsLettermethod(WordLengths.java:126) at WordLengths.testWordLengths(WordLengths.java:152)。 我想知道如果我无意中增加了限制,我得到了多少结果,否则我想不出什么是错的。走出界限错误

这是造成麻烦(max = freqs[i];)位:

 public int maxIndex(int[] freqs) { 
     int max = freqs[0]; 
     for (int i = 1; i < freqs.length; i++) { 
      if (freqs[i] > max) { 
       max = freqs[i]; 

      } 
     } 
     System.out.println("max number of words of certain lengths: "+max); 
     return freqs[max]; 

     //return max; 
    } 

这里的问题区域整个代码包括:

import edu.duke.FileResource; 

public class WordLengths { 


    public void countWordLengths(FileResource resource, int[] counts) { 
     String abc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
     for (String word : resource.words()) { 
      String trim = word.trim(); 
      int wordSize = trim.length(); 
      //System.out.println("word" + "\t" + trim); 
      int lastInd = trim.length()-1; 
      //System.out.println("lastIndexOf word" + "\t" + lastInd); 
      //abc.contains(trim.charAt(lastInd)); 
      char firstChar = trim.charAt(0); 
      char endChar = trim.charAt(trim.length()-1); 
      //System.out.println("firstChar" + "\t" + firstChar + "\t" + "endChar" + "\t" + endChar); 
      int idx = abc.indexOf(firstChar); 
      int idx_e = abc.indexOf(endChar); 
      int edx_sum = idx + idx_e; 
      //System.out.println("indexes abc" + "\t" + edx_sum); 
      //int idx_s = small.indexOf(firstChar); 
      //int idx_s_e = small.indexOf(endChar); 
      //int edx_s_sum = idx_s + idx_s_e; 
      //System.out.println("indexes small" + "\t" + edx_s_sum); 
      //System.out.println("indexes" + "\t" + idx + "\t" + idx_e + "\t" + idx_s + "\t" + idx_s_e); 
      //System.out.println("indexes" + "\t" + idx + "\t" + idx_e + "\t"); 

      if (idx == -1 && idx_e == -1) { 
       wordSize -= 2; 
      } else 
      if (idx == -1 || idx_e == -1) { 
       wordSize -= 1; 
      } 
      if(wordSize>=counts.length) { 
       counts[counts.length-1] += 1; 
      } else 
      //right algorithm 
      if(counts[wordSize] != 0) { 
       counts[wordSize] += 1; 

      } else { 
       counts[wordSize] = 1; 
      } 



     } 

     //test 
      /*for(int i : counts) { 
       System.out.println(i); 
      }*/ 
    } 
    /** 
    * the method countWordLengths(FileResource resource, int[] counts) with isLetter method 
    * 
    * @param resource 
    * @param counts 
    */ 
    public void countWordLengthsWithIsLettermethod(FileResource resource, int[] counts) { 
     for (String word : resource.words()) { 
      String trim = word.trim(); 
      int wordSize = trim.length(); 
      char firstChar = trim.charAt(0); 
      char endChar = trim.charAt(trim.length()-1); 
      if (!Character.isLetter(firstChar) && !Character.isLetter(endChar)) { 
       wordSize -= 2; 
      } else 
      if (!Character.isLetter(firstChar) || !Character.isLetter(endChar)) { 
       wordSize -= 1; 
      } 
      if(wordSize>=counts.length) { 
       counts[counts.length-1] += 1; 
      } else 
      //right algorithm 
      if(wordSize> 0 && counts[wordSize] != 0 ) { 
       counts[wordSize] += 1; 

      } else if (wordSize> 0) { 
       counts[wordSize] = 1; 

      } 
      System.out.println(counts[wordSize] + " words with length " + wordSize + ": " + word); 
     } 

     //test 
      /*for(int i : counts) { 
       System.out.println(i); 
      }*/ 
    } 
    public int maxIndex(int[] freqs) { 
     int max = freqs[0]; 
     for (int i = 1; i < freqs.length; i++) { 
      if (freqs[i] > max) { 
       max = freqs[i]; 

      } 
     } 
     System.out.println("max number of words of certain lengths: "+max); 
     return freqs[max]; 

     //return max; 
    } 
     public void testWordLengths() { 
     WordLengths wl = new WordLengths(); 
     FileResource fr = new FileResource(); 
     int counts[] = new int[100]; 
     //wl.countWordLengths(fr, counts); 
     wl.countWordLengthsWithIsLettermethod(fr, counts); 
     wl.maxIndex(counts); 
    } 
} 
+5

继续,告诉我们哪一行是'WordLengths.java:126'。 –

+0

请发布错误所针对的代码。 – hs2345

+0

我不确定它指向哪里。 “WordLengths.java:126”是否意味着它在第126行? – user5745776

回答

0

什么想到会发生,如果这个词是空的(或刚刚填满空白)

String trim = word.trim(); 
int wordSize = trim.length(); 
char firstChar = trim.charAt(0); 
char endChar = trim.charAt(trim.length()-1); 
+0

我不确定,这是造成错误的位? – user5745776