索引超出范围(文件读取)
Index Out of Range (File Reading)
我正在尝试让我的程序读取学生对多项选择题考试问题的回答(包含单独的文件),然后根据该文件打印出信息。
程序的输出应包括学生分数的完整列表(正确答案、错误答案和空白答案的数量),但我收到此错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: 9
at java.lang.String.charAt(String.java:658)
at ExamAnalysis.basicAnalysis(ExamAnalysis.java:38)
at ExamAnalysis.main(ExamAnalysis.java:31)
另外,如何使用 printf 打印一个普通的 int?现在,我正在为一些应该是整数的变量使用双精度数,因为我不知道如何打印整数。
代码:
import java.util.Scanner;
import java.util.Arrays;
import java.io.*;
public class ExamAnalysis {
public static void main(String[] args) throws FileNotFoundException {
Scanner keyboard = new Scanner(System.in);
double numStudents = 0;
System.out.print("Welcome to Exam Analysis. Let's begin ...\n"
+ "\nPlease type the correct answers to the exam questions,\n"
+ "one right after the other: ");
// String corrAns = keyboard.next();
String corrAns = "ABCEDBACED";
System.out
.print("\nWhat is the name of the file containing each student's\n"
+ "responses to the 10 questions?");
// File f = new File (keyboard.next());
File f = new File("exams.dat");
Scanner fileR1 = new Scanner(f);
while (fileR1.hasNextLine()) {
System.out.printf("Student #%.0f's responses: %s\n",
numStudents + 1, fileR1.nextLine());
numStudents++;
}
System.out
.printf("We have reached \"end of file!\"\n"
+ "\nThank you for the data on the %.0f students. Here's the analysis:\n",
numStudents);
String[] studentAns = new String[(int) numStudents];
Scanner fileR2 = new Scanner(f);
for (int num = 0; num < numStudents; num++) {
String ans = fileR2.nextLine();
studentAns[num] = ans;
}
int numStud = (int) numStudents;
basicAnalysis(numStud, studentAns, corrAns);
}
public static void basicAnalysis(int numStud, String[] studentAns,
String corrAns) {
int[][] bArray = new int[numStud][3];
for (int num = 0; num < numStud; num++) {
String ansString = studentAns[num];
for (int charNum = 0; charNum < corrAns.length(); charNum++) {
if (ansString.charAt(charNum) == ' ') {
bArray[num][2] += 1;
} else if (ansString.charAt(charNum) == corrAns.charAt(charNum)) {
bArray[num][0] += 1;
} else {
bArray[num][1] += 1;
}
}
}
System.out.println(Arrays.deepToString(bArray));
System.out.println("Student # Correct Incorrect Blank\n"
+ "~~~~~~~~~ ~~~~~~~ ~~~~~~~~~ ~~~~~");
for (int numb = 0; numb < numStud; numb++) {
System.out.printf("%5.0f %11d %11d %8d\n", (double) numb + 1,
bArray[numb][0], bArray[numb][1], bArray[numb][2]);
}
}
}
我正在读取的文件是:
ABDEBBAC D
ABCE CACED
DCE AEDC
ABCEB ACED
BBCEDBACED
DBCE CACED
ABCE CA E
BBE CACED
ABCEDBACED
我很确定我的方法 basicAnalysis 工作正常。可能与空白阅读有关。
任何帮助将不胜感激。谢谢
不确定我是否完全理解您要完成的任务,但是如果 exams.dat 中的所有记录都必须具有相同的长度,那么倒数第 3 个记录将缺少一个字符。
我指的是那条记录:
ABCE CA E
所有其他记录都是 10 个字符长,但只有 9 个字符。
更新:
为了使其更加稳健,您似乎需要考虑以下事实,即学生的答案数量可能与预期(正确)答案不同,并且两种方式(即学生回答少于或多于预期的答案)。
我会将你内部 for
循环体的内容更改为:
if (charNum >= ansString.length()) {
bArray[num][2] += 1;
} else if (ansString.charAt(charNum) == ' ') {
bArray[num][2] += 1;
} else if (ansString.charAt(charNum) == corrAns.charAt(charNum)) {
bArray[num][0] += 1;
} else {
bArray[num][1] += 1;
}
如果您想在学生回答多于预期时增加错误回答的数量,您只需要在内部 for
循环之前或之后添加:
if (ansString.length() > corrAns.length())
bArray[num][2] += (ansString.length() - corrAns.length());
你的输入文件有问题
而不是
for (int charNum = 0; charNum < corrAns.length(); charNum++)
每次你用corrAns
检查。最多检查 ansString
。如果您有更多记录,则很难更改具有 9
个字符的内容。
使用
for (int charNum = 0; charNum < ansString.length(); charNum++) {
我正在尝试让我的程序读取学生对多项选择题考试问题的回答(包含单独的文件),然后根据该文件打印出信息。
程序的输出应包括学生分数的完整列表(正确答案、错误答案和空白答案的数量),但我收到此错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: 9
at java.lang.String.charAt(String.java:658)
at ExamAnalysis.basicAnalysis(ExamAnalysis.java:38)
at ExamAnalysis.main(ExamAnalysis.java:31)
另外,如何使用 printf 打印一个普通的 int?现在,我正在为一些应该是整数的变量使用双精度数,因为我不知道如何打印整数。
代码:
import java.util.Scanner;
import java.util.Arrays;
import java.io.*;
public class ExamAnalysis {
public static void main(String[] args) throws FileNotFoundException {
Scanner keyboard = new Scanner(System.in);
double numStudents = 0;
System.out.print("Welcome to Exam Analysis. Let's begin ...\n"
+ "\nPlease type the correct answers to the exam questions,\n"
+ "one right after the other: ");
// String corrAns = keyboard.next();
String corrAns = "ABCEDBACED";
System.out
.print("\nWhat is the name of the file containing each student's\n"
+ "responses to the 10 questions?");
// File f = new File (keyboard.next());
File f = new File("exams.dat");
Scanner fileR1 = new Scanner(f);
while (fileR1.hasNextLine()) {
System.out.printf("Student #%.0f's responses: %s\n",
numStudents + 1, fileR1.nextLine());
numStudents++;
}
System.out
.printf("We have reached \"end of file!\"\n"
+ "\nThank you for the data on the %.0f students. Here's the analysis:\n",
numStudents);
String[] studentAns = new String[(int) numStudents];
Scanner fileR2 = new Scanner(f);
for (int num = 0; num < numStudents; num++) {
String ans = fileR2.nextLine();
studentAns[num] = ans;
}
int numStud = (int) numStudents;
basicAnalysis(numStud, studentAns, corrAns);
}
public static void basicAnalysis(int numStud, String[] studentAns,
String corrAns) {
int[][] bArray = new int[numStud][3];
for (int num = 0; num < numStud; num++) {
String ansString = studentAns[num];
for (int charNum = 0; charNum < corrAns.length(); charNum++) {
if (ansString.charAt(charNum) == ' ') {
bArray[num][2] += 1;
} else if (ansString.charAt(charNum) == corrAns.charAt(charNum)) {
bArray[num][0] += 1;
} else {
bArray[num][1] += 1;
}
}
}
System.out.println(Arrays.deepToString(bArray));
System.out.println("Student # Correct Incorrect Blank\n"
+ "~~~~~~~~~ ~~~~~~~ ~~~~~~~~~ ~~~~~");
for (int numb = 0; numb < numStud; numb++) {
System.out.printf("%5.0f %11d %11d %8d\n", (double) numb + 1,
bArray[numb][0], bArray[numb][1], bArray[numb][2]);
}
}
}
我正在读取的文件是:
ABDEBBAC D
ABCE CACED
DCE AEDC
ABCEB ACED
BBCEDBACED
DBCE CACED
ABCE CA E
BBE CACED
ABCEDBACED
我很确定我的方法 basicAnalysis 工作正常。可能与空白阅读有关。 任何帮助将不胜感激。谢谢
不确定我是否完全理解您要完成的任务,但是如果 exams.dat 中的所有记录都必须具有相同的长度,那么倒数第 3 个记录将缺少一个字符。 我指的是那条记录:
ABCE CA E
所有其他记录都是 10 个字符长,但只有 9 个字符。
更新:
为了使其更加稳健,您似乎需要考虑以下事实,即学生的答案数量可能与预期(正确)答案不同,并且两种方式(即学生回答少于或多于预期的答案)。
我会将你内部 for
循环体的内容更改为:
if (charNum >= ansString.length()) {
bArray[num][2] += 1;
} else if (ansString.charAt(charNum) == ' ') {
bArray[num][2] += 1;
} else if (ansString.charAt(charNum) == corrAns.charAt(charNum)) {
bArray[num][0] += 1;
} else {
bArray[num][1] += 1;
}
如果您想在学生回答多于预期时增加错误回答的数量,您只需要在内部 for
循环之前或之后添加:
if (ansString.length() > corrAns.length())
bArray[num][2] += (ansString.length() - corrAns.length());
你的输入文件有问题
而不是
for (int charNum = 0; charNum < corrAns.length(); charNum++)
每次你用corrAns
检查。最多检查 ansString
。如果您有更多记录,则很难更改具有 9
个字符的内容。
使用
for (int charNum = 0; charNum < ansString.length(); charNum++) {