0

我正在尝试解决程序中的问题,但无法弄清楚如何解决它。我相信问题出在我试图放置报告输出的方式上 - 显示 ID #,然后是 name: lastname, firstname 当运行报告时,让 Sys.out.print 行说“学生姓名” + 数组 [2] + 数组 [1]。

我该如何解决这个问题?它仅在我尝试运行报告时发生。输入学生信息时创建的文本文件已正确完成。

/** * 文件名:LowerAndUpperDisplay.java * 创建时间:2013 年 10 月 31 日 * 创建者: * 目的:扩展 LowerAndUpper 应用程序,以便它从 lowerclassmanenter code here文件中逐一显示每个记录,然后从 upperclassman 文件中显示。*显示标题以介绍每个列表。* * 对于每条记录,显示 ID 号、名字、姓氏、获得的学分以及毕业所需的额外学分 *。假设毕业需要 120 个学分。*/

import java.util.Scanner;
import java.nio.file.*;
import java.io.*;

public class LowerAndUpperDisplay {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        Path lowerClassmanFile = Paths.get("C:/temp/lowerclassman.txt");

        Path upperClassmanFile = Paths.get("C:/temp/upperclassman.txt");

        char choice;

        int idNum;

        int totalCredits;

        String first;

        String last;

        String classStanding = " ";

        try

        {
            OutputStreamWriter lWriter = new OutputStreamWriter(
                    new BufferedOutputStream(Files.newOutputStream(
                            lowerClassmanFile, StandardOpenOption.CREATE)));

            BufferedWriter lowerClassmanWriter = new BufferedWriter(lWriter);

            OutputStreamWriter uWriter = new OutputStreamWriter(
                    new BufferedOutputStream(Files.newOutputStream(
                            upperClassmanFile, StandardOpenOption.CREATE)));

            BufferedWriter upperClassmanWriter = new BufferedWriter(uWriter);

            do {

                System.out
                        .print("Student Records "
                                + "\nTo enter student records, type S.  For a report, type R.  To exit, type Q.");

                choice = input.nextLine().charAt(0);

                switch (choice)

                {
                case 'S':

                    System.out.println("Please enter student information.");

                    System.out.print("ID Number: ");

                    idNum = input.nextInt();

                    input.nextLine();

                    System.out.print("First Name: ");

                    first = input.nextLine();

                    System.out.print("Last Name: ");

                    last = input.nextLine();

                    System.out.println("Credit Hours Earned: ");

                    totalCredits = input.nextInt();

                    input.nextLine();

                    classStanding = "ID Number " + idNum + ", Student Name: "
                            + last + ", " + first + " Credit Hours: "
                            + totalCredits;

                    if (totalCredits >= 60) {

                        upperClassmanWriter.write(classStanding);

                        upperClassmanWriter.newLine();
                    }

                    else {

                        lowerClassmanWriter.write(classStanding);

                        lowerClassmanWriter.newLine();
                    }

                    break;

                case 'R':

                    try {

                        InputStreamReader lReader = new InputStreamReader(
                                new BufferedInputStream(Files
                                        .newInputStream(lowerClassmanFile)));

                        BufferedReader reader1 = new BufferedReader(lReader);

                        InputStreamReader uReader = new InputStreamReader(
                                new BufferedInputStream(Files
                                        .newInputStream(upperClassmanFile)));

                        BufferedReader reader2 = new BufferedReader(uReader);

                        System.out
                                .print("The lower classmen are:  ");

                        classStanding = reader1.readLine();

                        String[] array;

                        while (classStanding != null) {

                            array = classStanding.split(",");

                            totalCredits = Integer.parseInt(array[3]);

                            System.out.print("ID Number " + array[0]);

                            System.out.print("Student name " + array[2] + ", " + array[1]) ;

                            System.out.print("Total credits: " + totalCredits);

                            System.out.print("This student is " + (120 - totalCredits) 
                                    + " credits short of earning their degree.");

                            classStanding = reader1.readLine();

                        }// end while

                        reader1.close();

                        System.out.print("The upper classmen are:  ");

                        classStanding = reader2.readLine();

                        while (classStanding != null) {

                            array = classStanding.split(",");

                            totalCredits = Integer.parseInt(array[3]);

                            //System.out.format("Student Id Number :", "");

                            System.out.print("ID Number " + array[0]);

                            System.out.print("Student name : " + array[2] + ", " + array[1]);

                            System.out.print("Total credits: " + totalCredits);

                            System.out.print("This student is  " + (120 - totalCredits) 
                                    + "credits short of earning their degree.");

                            classStanding = reader2.readLine();

                        }// end while

                        reader2.close();

                    } catch (Exception e) {

                        System.out.println("Message: " + e);

                    }
                case 'Q':

                    break;

                default:

                    System.out
                            .printf("You have made an invalid option.  Please enter a valid option. \n");

                }// end switch

            } while (choice != 'Q');

            lowerClassmanWriter.close();

            upperClassmanWriter.close();
        } catch (Exception e)

        {
            System.out
                    .println("The program has encountered an error.  Please notify your Systems Administrator"
                            + " of this error. " + e);
        }

    }// end

}// end class 
4

1 回答 1

0

ArrayIndexOutOfBounds

classStanding = "ID Number " + idNum + ", Student Name: "
                        + last + ", " + first + " Credit Hours: "
                        + totalCredits;

array = classStanding.split(",");

totalCredits = Integer.parseInt(array[3]);

数组只有 3 个索引。如果你想要最后一个,你应该打电话array[2]而不是array[3].

也是array[2]一个字符串" Credit Hours: " + totalCredits;。你无法解析它。你应该split再次使用来获得totalCredits

于 2013-11-03T22:31:29.857 回答