无法在Java程序中拆分数据

时间:2014-11-23 01:37:26

标签: java

我试图拆分我的变量并输入我的学生数据,所以我可以继续我的成绩计算程序,但我第二次拆分输入的字符串,有一个问题,我无法弄清楚是什么它的原因。

用户将输入如下所示的信息 John Denver:e100 q70 q50 h100 e100 e90 h80 q60 h100

程序需要将所有这些数据拆分并将名称输入到数组中,然后将考试分数,测验分数和家庭作业分数表示为" e"," q& #34;或" h"在输入的数据中。

import java.util.Scanner;

public class GradeCalcWithArrays { /*
                                 * Daniel The purpose is to calculate
                                 * entered grades
                                 */
public static void main(String[] args) {

    Scanner s = new Scanner(System.in);

    boolean done = false;
    boolean quit = false;
    int choice = 0;
    int maxstudents = 200;

    int[] examstats = new int[3]; /*
                                 * Array created to store the information
                                 * entered for exams
                                 */
    int[] quizstats = new int[3]; /*
                                 * Array created to store the information
                                 * entered for quizzes
                                 */
    int[] homeworkstats = new int[3]; /*
                                     * Array created to store the
                                     * information entered for homework
                                     */

    String[] studentnames = new String[maxstudents]; /*
                                                     * Array created to
                                                     * store the student
                                                     * name information
                                                     * entered
                                                     */

    System.out.println("Welcome to GradeBook!");
    System.out.println("Please provide grade item details");

    System.out.print("Exams    (number, points, weight):");

    examstats[0] = s.nextInt(); // inputs exam number
    examstats[1] = s.nextInt(); // inputs exam points
    examstats[2] = s.nextInt(); // inputs exam weight

    System.out.print("Quizzes     (number, points, weight):");

    quizstats[0] = s.nextInt(); // inputs quiz number
    quizstats[1] = s.nextInt(); // inputs quiz points
    quizstats[2] = s.nextInt(); // inputs quiz weight

    System.out.print("Homework    (number, points, weight):");

    homeworkstats[0] = s.nextInt(); // inputs homework number
    homeworkstats[1] = s.nextInt(); // inputs homework points
    homeworkstats[2] = s.nextInt(); // inputs homework weight

    double[] examscores = new double[examstats[0]];
    double[] quizscores = new double[quizstats[0]];
    double[] hwscores = new double[homeworkstats[0]];

    System.out.println("--------------------");

    do {
        System.out.println("What would you like to do?");
        System.out.println("    1 Add student data");
        System.out.println("    2 Display student grades & statistics");
        System.out.println("    3 Plot grade distribution");
        System.out.println("    4 Quit");
        System.out.print("Your choice:");
        choice = s.nextInt(); /*
                             * Choice will determine what the next course of
                             * action will be with the program
                             */

        if (choice == 1) {
            System.out.println("Enter student data:");

            for (int i = 0; i <= maxstudents; i++) {
                System.out.print("Data>");
                String dataentry = s.nextLine();

                String[] firstsplit = dataentry.split(":");
                studentnames[i] = firstsplit[0];
                String[] secondsplit = firstsplit[1].split(" ");

                for (int j = 0; j <= maxstudents; j++) {

                    String c;

                    c = secondsplit[j].substring(0, 1);

                    if (c == "e") {
                        secondsplit = secondsplit[j].split("e");
                        examscores[i] = Double.parseDouble(secondsplit[j]);

                    }
                    if (c == "q") {
                        secondsplit = secondsplit[j].split("q");
                        quizscores[i] = Double.parseDouble(secondsplit[j]);

                    }
                    if (c == "h") {
                        secondsplit = secondsplit[j].split("h");
                        hwscores[i] = Double.parseDouble(secondsplit[j]);

                    }

                    if (dataentry.equals("done")) {
                        break;
                    }
                }
            }
        }

        if (choice == 2) {

        }

        if (choice == 3) {

        }

        if (choice == 4) {
            quit = true;
            System.out.println("Good bye!");
        }

    } while (quit == false);

}

}

我收到一条错误消息:线程中的异常&#34; main&#34; java.lang.ArrayIndexOutOfBoundsException:1     在GradeCalcWithArrays.main(GradeCalcWithArrays.java:83)

有人可以帮我解决这个问题,并告诉我我的程序是否可行?

1 个答案:

答案 0 :(得分:0)

有一件事是你有maxstudents设置为200,你分配的学生名数组大小为200,但这意味着有效的下标是0到199,但你的for循环使用小于或等于测试200:

        `for (int i = 0; i <= maxstudents; i++)` 

这意味着i在最后一次迭代中为200,因此在该迭代中,您的代码将有效地执行studentnames[200] = firstsplit[0];,这将导致ArrayIndexOutOfBoundsException。

但是,如果在不超出for循环的情况下达到第200次迭代,则上述问题将会出现问题。看一下代码,我确实看到你试图在输入“完成”时中断,但是这个break语句实际上是在嵌套的for循环中,所以它不会突破外部循环。该测试确实应该在您进行第一次拆分之前进行。如果输入“完成”,没有理由甚至尝试拆分,所以也没有理由进入嵌套的for循环。

还有其他问题:嵌套for循环不应该针对maxstudents进行测试,因为它是您关心的成绩数。我会留下其余部分让你弄清楚 - 提示其中一个:你应该检查输入是否有错误。