我的程序出了什么问题 - 错误消息但工作正常?

时间:2015-06-10 00:09:26

标签: java arraylist

我的程序完全按照我想要的方式运行,但是我在控制台中收到错误消息:

  

线程中的异常" main" java.lang.IndexOutOfBoundsException:索引:   6,尺寸:6

     

at java.util.ArrayList.rangeCheck(ArrayList.java:653)

     

at java.util.ArrayList.get(ArrayList.java:429)

     

在Kevinmath4.checkAnswers(Kevinmath4.java:152)

     

在Kevinmath4.main(Kevinmath4.java:39)

以下是代码:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class Kevinmath4 {
    static File filename = new File("homework.txt");
    static ArrayList<Object> aList = new ArrayList<Object>();
    static String homework = "";
    static File filename2 = new File("homework2.txt");
    static ArrayList<Object> aList2 = new ArrayList<Object>();
    static String homework2 = "";
    static String answerPass = "";
    static ArrayList<Object> aList3 = new ArrayList<Object>();
    static final int TOTAL_QUESTIONS = 5;
    static String date;

    public static void main(String[] args) throws FileNotFoundException {
        String initialInput = JOptionPane.showInputDialog(null,
                "Enter Add answers / Check answers to continue");

        switch (initialInput) {
        case "Add answers":
            answers("excalibur117", aList, filename);
            break;

        // Need to store the array permanently
        case "Check answers": // Need to make it so it stores array of
            // Kevin's answers permanently
            clearFile(filename2);
            answers("Kevin", aList2, filename2);
            readAnswers(filename, aList3);
            checkAnswers(aList3, aList2);
            break;

        default:
            JOptionPane.showMessageDialog(null, "Please enter a valid option.");
            break;
        }
        clearFile(filename2);

        // exit the program
        JOptionPane.showMessageDialog(null,
                "Thanks for using this program. Cheers!");
    }

    public static void answers(String pass, ArrayList<Object> list, File f) {

        answerPass = JOptionPane.showInputDialog(null,
                "Please enter the password");
        // validate user
        while (!answerPass.equals(pass)) {
            JOptionPane.showMessageDialog(null,
                    "Incorrect Password. Please try again.");
            answerPass = JOptionPane.showInputDialog(null,
                    "Please enter the password.");
        }

        // add answers
        String final1 = "";
        do {
            list.clear();

            // validate the date of the answers
            date = JOptionPane.showInputDialog(null,
                    "Enter the date of the desired" + " answers (MM/DD/YY)");
            // add your answers
            enterAnswers(date, list, f);

            // verify the answers
            final1 = JOptionPane.showInputDialog(null, "Is this correct: "
                    + list.get(1) + " " + list.get(2) + " " + list.get(3) + " "
                    + list.get(4) + " " + list.get(5) + "? (Yes/No)");

        } while (final1.charAt(0) == 'n' || final1.charAt(0) == 'N');
    }

    public static void enterAnswers(String options, ArrayList<Object> list,
            File f) {

        do {
            boolean valid = false;
            list.add(date);
            for (int i = 0; i < 5; i++) {
                while (!valid) {
                    homework = JOptionPane
                            .showInputDialog("Please enter your answer for question "
                                    + (i + 1));
                    if (!homework.isEmpty())
                        valid = true;
                }
                list.add(homework);
                valid = false;
            }

            writeFile(f, list); // write the answers to a file

            break;

        } while (!homework.isEmpty());
    }

    public static void writeFile(File filename, ArrayList<Object> list) {
        try {
            FileWriter fw = new FileWriter(filename, true);
            Writer output = new BufferedWriter(fw);

            for (int j = 0; j < list.size(); j++) {
                output.write(list.get(j) + "\n");
            }
            output.close();

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    "Oops! I cannot create that file.");
        }
    }

    public static void clearFile(File filename) {
        try {
            FileWriter fw = new FileWriter(filename, false);
            Writer output = new BufferedWriter(fw);
            output.write("");
            output.close();

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    "Oops! I cannot create that file.");
        }
    }

    public static void checkAnswers(ArrayList<Object> a, ArrayList<Object> b) {
        int i = 1; // counter variable
        int j = a.indexOf(date);
        for (Object obj : a) { // iterate through any list
            for (int k = (j + 1); k < (j + 6); k++) {
                if (obj.getClass() == String.class) { // find if it's a
                    // string
                    if (!a.get(k).equals(b.get(i))) {
                        JOptionPane.showMessageDialog(null, "#" + (i)
                                + " is wrong.");
                    }
                }

                if (obj.getClass() == Double.class) { // or a double
                    if (!a.get(k).equals(b.get(i))) {
                        JOptionPane.showMessageDialog(null, "#" + (i)
                                + " is wrong.");
                    }
                }

                if (obj.getClass() == Integer.class) { // or an integer
                    if (!a.get(k).equals(b.get(i))) {
                        JOptionPane.showMessageDialog(null, "#" + (i)
                                + " is wrong.");
                    }
                }
                i++;

            }
        }
    }

    public static void readAnswers(File filename, ArrayList<Object> list)
            throws FileNotFoundException {
        Scanner s = new Scanner(new File("homework.txt"));
        while (s.hasNextLine()) {
            list.add(s.nextLine());
        }
        s.close();
    }
}

我做错了什么?

2 个答案:

答案 0 :(得分:1)

问题是你的for循环,

for (int k = (j + 1); k < (j + 6); k++) {

当您(盲目地)访问a.get(k)时,您 知道 k < a.size()(如果它== a.size()你是&#39 ;得到你发布的例外)。我想你想要,

for (int k = (j + 1); k < a.size(); k++) {

答案 1 :(得分:1)

一般来说,听起来像'#34;我的代码完成它应该做的事情,然后我得到了一个例外&#34;,特别是在学术工作中,通常意味着你已经迭代了一个循环一次太多次了。这种情况通过例外ArrayIndexOutOfBoundsException加强。

考虑数组{a,b,c}。到目前为止,我确定您知道所述数组的第0位的值是&#39; a&#39;,第1位包含&#39; b&#39;,第2位包含&#39; C&#39 ;.如果你在Java中尝试访问这个数组的第3位,你将得到一个ArrayIndexOutOfBoundsException,因为在第3位没有元素,并且数组不长。

我要走出困境,并猜测第152行是这一行: if (!a.get(k).equals(b.get(i)))

我想猜测当k == 6时a.get(k)被调用,但a只有长度6(这意味着它的最高索引是5)。

我猜测表达式for (int k = (j + 1); k < (j + 6); k++)是罪魁祸首,我猜这个表达式应该是for (int k = (j + 1); k < (j + 5); k++)

但是,我只能猜测,因为,坦率地说(而不是试图粗鲁......只是说...),你的代码是一团糟。更好的变量名称,注释以及较少使用魔法数字将会带你走远。

希望这有帮助。