【发布时间】:2016-12-08 18:56:42
【问题描述】:
我有一个文本文件,其中一行是学生姓名,下一行是 15 个成绩(每个成绩由 2 个空格分隔,并且在行首有一个空格)。
示例:
John, Elton
89 97 91 94 82 96 98 67 69 97 87 90 100 80 92
Johnson, Kay
96 68 71 70 90 79 69 63 88 75 61 77 100 80 88
(More data...)
我需要读取包含学生姓名的行并将值存储在字符串数组中,然后读取以下成绩行,将值存储在二维整数数组中。在每行整数中,前 10 个值将被视为作业成绩,而后 5 个值是考试成绩(稍后在计算学生平均成绩时使用)。当我的程序尝试读取第一个学生姓名之后的整数行时,由于“输入不匹配异常”而崩溃。到目前为止,这是我的代码:
import java.io.*;
import java.util.*;
public class Grades
{
// Declare constants for array lengths
private static final int ROWS = 25;
private static final int COLS = 15;
public static void main(String[] args) throws IOException
{
// Create new two dimensional integer array to hold students assignment & exam grades
int[][] grades = new int[ROWS][COLS];
// Create new string array to hold student names
String[] students = new String[ROWS];
// Total number of students
int numStudents;
// Display your name
System.out.println( "YOUR NAME" );
// Fill the arrays
numStudents = fillArray( grades, students );
// Display the data that was just read in
display(grades, students, numStudents);
}
// The fillArray method opens & reads student name & grade data from a text file
private static int fillArray(int[][] sGrades, String[] sNames) throws IOException
{
// Students counter
int students = 0;
// Create the file
File studentGrades = new File("Grades.txt");
// Check that the file exists
if (!studentGrades.exists())
{
// Display error message and exit the program
System.out.println(studentGrades + " not found. Aborting.");
System.exit(0);
}
// Create scanner object to read from the file
Scanner gradeFile = new Scanner(studentGrades);
// Read data until the end of the file is reached or the array is full
while (gradeFile.hasNext() && students < sNames.length)
{
// Begin filling the array of student names starting with the first element
for ( int i = 0; i < ROWS; i++ )
{
// Store the students name into the array's current index
sNames[i] = gradeFile.nextLine();
// Increment the number of students
students++;
// Begin filling in the array of grades starting with the first element
/************* ERROR OCCURS HERE ********************/
for ( int j = 0; j < COLS; j++ )
// Store the grade into the array's current index
sGrades[i][j] = gradeFile.nextInt();
/************** *******************/
}
}
// Close the file
gradeFile.close();
// Return the total number of students
return students;
}
// The display method prints Each student's name, 10 assignment
// grades and 5 exam grades
// to the screen, separating each student by a blank line.
total number of students
private static void display(int[][] gradeArray, String [] studentArray, int totalStudents)
{
//
for ( int i = 0; i < totalStudents; i++)
{
System.out.println();
//
System.out.print("STUDENT: " + studentArray[i]);
System.out.print("\nASSIGNMENTS: ");
//
for ( int j = 0; j < 10; j++)
//
System.out.print(gradeArray[i][j] + " ");
//
System.out.print("\nEXAMS: ");
//
for ( int k = 10; k < COLS; k++)
//
System.out.print(gradeArray[i][k] + " ");
System.out.println();
}
'display' 方法的输出应如下所示:
YOUR NAME
STUDENT: John, Elton
ASSIGNMENTS: 89 97 91 94 82 96 98 67 69 97
EXAMS: 87 90 100 80 92
STUDENT: Johnson, Kay
ASSIGNMENTS: 96 68 71 70 90 79 69 63 88 75
EXAMS: 61 77 100 80 88
(More output...)
由于程序在我看到任何输出之前就崩溃了,因此我删除了以下试图读取下一个 Integer 以查看发生了什么的代码行。
for ( int j = 0; j < COLS; j++ )
sGrades[i][j] = gradeFile.nextInt();
正如我所料,文件的每一行都作为字符串读取,值存储在学生姓名数组中,输出如下:
STUDENT: John, Elton
ASSIGNMENTS: 0 0 0 0 0 0 0 0 0 0
EXAMS: 0 0 0 0 0
STUDENT: 89 97 91 94 82 96 98 67 69 97 87 90 100 80 92
ASSIGNMENTS: 0 0 0 0 0 0 0 0 0 0
EXAMS: 0 0 0 0 0
STUDENT: Johnson, Kay
ASSIGNMENTS: 0 0 0 0 0 0 0 0 0 0
EXAMS: 0 0 0 0 0
STUDENT: 96 68 71 70 90 79 69 63 88 75 61 77 100 80 88
ASSIGNMENTS: 0 0 0 0 0 0 0 0 0 0
EXAMS: 0 0 0 0 0
(More output...)
试图弄清楚我哪里出错了,我的大脑很痛。如果我猜测,我认为这与分隔每个整数的空格有关,导致每行成绩被解释为字符串而不是整数。或者还有什么我可能忽略的?
编辑:我仅限于对这个程序使用更基本的 Java 技术。除了方法头中的throws 子句之外,我不能使用数组列表和异常处理。文件处理仅限于使用 File 和 Scanner 类。没有BufferReader 或StringReader 或FileReader。
【问题讨论】:
标签: java arrays string file int