[英]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)
如果wordCount
为0
则[wordCount - 1]
为-1
所以当访问-1
index时,您将获得ArrayIndexOutOfBoundsException: -1
异常。 count[wordCount - 1]//error occured
为了避免首先检查此wordCount - 1
长度。仅在wordCount > 0
时wordCount > 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.