繁体   English   中英

此错误对我的项目意味着什么?

[英]What does this error mean in my project?

您好,我正在一个项目上,但我继续收到错误消息,但我不知道为什么。 有人可以帮忙吗?

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)

如果wordCount0[wordCount - 1]-1

所以当访问-1 index时,您将获得ArrayIndexOutOfBoundsException: -1异常。 count[wordCount - 1]//error occured

为了避免首先检查此wordCount - 1长度。仅在wordCount > 0wordCount > 0 wordCount - 1

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++;
        }
    }
}

我相信我已修正您的代码。 现在应该可以正常运行了。 您需要更改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) {
               } 
            }
        }
     }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM