这个错误在我的项目中意味着什么?

时间:2015-08-01 13:41:24

标签: java

您好我正在处理一个项目,我继续收到错误消息,我无法弄清楚原因。有人可以帮忙吗?

import java.util.Scanner;
import java.util.ArrayList;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.lang.StringIndexOutOfBoundsException;


public class Authorship{

     public static void main(String []args){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Name of input file:");
        String correctAnswers = scanner.next();
        File file =new File(scanner.next());
        if(!file.exists()){
            System.out.println(" This file does not exist");
        } else {
            BufferedReader reader = null;
            ArrayList<String> list = new ArrayList<String>();
            try {
                reader = new BufferedReader(new FileReader(file));
                String text = null;
                int count[] = new int[13];
                while ((text = reader.readLine()) != null) {
                    String[] line = text.split(" ");
                    for(int i=0;i<line.length;i++){
                        int wordCount = line[i].length();
                        count[wordCount-1]++;
                        totalWordCount++;
                    }
                }
                for(int i = 0;i < 13;i++){
                  float percentage =count[i]*100/totalWordCount;
                  if(i != 12) {
                        System.out.printf("Proportion of "+(i+1)+"-letter  words: %.2f%%(%d words)", percentage, count[i]);
             } else {
                        System.out.printf("Proportion of 13- (or more)letter words: %.2f%%(%d words)", percentage, count[i]);
                        System.out.println("\n");
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                 } catch (IOException e) {
               } 
            }
        }
     }
  }
}

我收到错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at Authorship.main(Authorship.java:36)

2 个答案:

答案 0 :(得分:4)

如果wordCount0,则[wordCount - 1]-1

因此,在访问ArrayIndexOutOfBoundsException: -1索引时,您会收到-1异常。 count[wordCount - 1]//error occured

要避免此检查wordcount长度first.access wordCount - 1仅在wordCount > 0

while ((text = reader.readLine()) != null) {
    String[] line = text.split(" ");
    for (int i = 0; i < line.length; i++) {
        int wordCount = line[i].length();
        if (wordCount > 0) {
            count[wordCount - 1]++;
            totalWordCount++;
        }
    }
}

答案 1 :(得分:0)

我相信我修复了你的代码。它现在应该运行良好。 你需要改变totalWordCount ++;到wordCount和* 100 / totalWordCount;我

import java.util.Scanner;
import java.util.ArrayList;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.lang.StringIndexOutOfBoundsException;


public class Authorship{

     public static void main(String []args){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Name of input file:");
        String correctAnswers = scanner.next();
        File file =new File(scanner.next());
        if(!file.exists()){
            System.out.println(" This file does not exist");
        } else {
            BufferedReader reader = null;
            ArrayList<String> list = new ArrayList<String>();
            try {
                reader = new BufferedReader(new FileReader(file));
                String text = null;
                int count[] = new int[13];
                while ((text = reader.readLine()) != null) {
                    String[] line = text.split(" ");
                    for(int i=0;i<line.length;i++){
                        int wordCount = line[i].length();
                        count[wordCount-1]++;
                        wordCount++;
                    }
                }
                for(int i = 0;i < 13;i++){
                  float percentage =count[i]*100/i;
                  if(i != 12) {
                        System.out.printf("Proportion of "+(i+1)+"-letter  words: %.2f%%(%d words)", percentage, count[i]);
             } else {
                        System.out.printf("Proportion of 13- (or more)letter words: %.2f%%(%d words)", percentage, count[i]);
                        System.out.println("\n");
                    }
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                 } catch (IOException e) {
               } 
            }
        }
     }
  }