gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:35:57 24 4
gpt4 key购买 nike

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

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 6, Size: 6

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

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

at Kevinmath4.checkAnswers(Kevinmath4.java:152)

at 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();
}
}

我做错了什么?

最佳答案

问题是你的for在这里循环,

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

当您(盲目地)访问a.get(k)时你不知道k < a.size() (如果是== a.size(),您将收到发布的异常)。我想你想要,

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

关于java - 我的程序出了什么问题 - 出现错误消息但工作正常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30744983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com