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

Java 这个错误在我的项目中意味着什么?,java,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.

您好,我正在做一个项目,我继续收到一条错误消息,我不知道为什么。有人能帮忙吗

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) {
               } 
            }
        }
     }
  }
}

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

因此,当访问
-1
索引时,您将获得
ArrayIndexOutOfBoundsException:-1
异常<代码>计数[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++;
        }
    }
}
while((text=reader.readLine())!=null){
String[]line=text.split(“”);
对于(int i=0;i0){
计数[wordCount-1]+;
totalWordCount++;
}
}
}

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

因此,当访问
-1
索引时,您将获得
ArrayIndexOutOfBoundsException:-1
异常<代码>计数[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++;
        }
    }
}
while((text=reader.readLine())!=null){
String[]line=text.split(“”);
对于(int i=0;i0){
计数[wordCount-1]+;
totalWordCount++;
}
}
}

我想我已经修复了您的代码。现在应该运行良好了。 您需要更改totalWordCount++;至字数和*100/总字数;对我来说

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) {
               } 
            }
        }
     }
  }
import java.util.Scanner;
导入java.util.ArrayList;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.io.File;
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.lang.StringIndexOutOfBoundsException;
公共类作者{
公共静态void main(字符串[]args){
扫描仪=新的扫描仪(System.in);
System.out.println(“输入文件的名称:”);
字符串correctAnswers=scanner.next();
File File=新文件(scanner.next());
如果(!file.exists()){
System.out.println(“此文件不存在”);
}否则{
BufferedReader reader=null;
ArrayList=新建ArrayList();
试一试{
reader=newbufferedreader(newfilereader(file));
字符串文本=空;
整数计数[]=新整数[13];
而((text=reader.readLine())!=null){
String[]line=text.split(“”);

对于(inti=0;i我相信我修复了您的代码。它现在应该可以正常运行了。 您需要将totalWordCount++;更改为wordCount,将*100/totalWordCount;更改为i

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) {
               } 
            }
        }
     }
  }
import java.util.Scanner;
导入java.util.ArrayList;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.io.File;
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.lang.StringIndexOutOfBoundsException;
公共类作者{
公共静态void main(字符串[]args){
扫描仪=新的扫描仪(System.in);
System.out.println(“输入文件的名称:”);
字符串correctAnswers=scanner.next();
File File=新文件(scanner.next());
如果(!file.exists()){
System.out.println(“此文件不存在”);
}否则{
BufferedReader reader=null;
ArrayList=新建ArrayList();
试一试{
reader=newbufferedreader(newfilereader(file));
字符串文本=空;
整数计数[]=新整数[13];
而((text=reader.readLine())!=null){
String[]line=text.split(“”);

对于(int i=0;错误来自于您试图以不可能的索引访问数组的事实(例如,-1或索引>=length)。
count[wordCount-1]
wordCount==0
行是什么:36?我尝试了,现在我有11个错误。错误来自于您试图以不可能的索引访问数组的事实(例如-1或索引>=length)。
count[wordCount-1]
wordCount==0
行是什么:36?我尝试过,现在我有11个错误