2015-06-10 12 views
0

我的程序運行的正是我希望它的方式,但我在控制檯中的錯誤消息:在線程我的程序有什麼問題 - 錯誤信息但工作正常?

異常「主要」 java.lang.IndexOutOfBoundsException:指數: 6,尺寸:6

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

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

我在做什麼錯?

+3

請問哪行代碼是152行? –

+0

你認爲這個例外意味着什麼? –

回答

1

的問題是你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++) { 
1

一般地講,這聽起來像「我的代碼做的事情是應該的,但後來我得到一個異常反正」,特別是在學術工作的情況下,通常意味着你已經迭代循環太多次。這是由例外類型ArrayIndexOutOfBoundsException加強的。考慮數組{a,b,c}。現在,我相信你知道所述數組的位置0的值是'a',位置1包含'b',位置2包含'c'。如果您嘗試使用Java訪問此數組的位置3,將會得到一個ArrayIndexOutOfBoundsException,因爲在位置3沒有元素,並且該數組沒有那麼長。

我要去無路可退,並猜測該行152是這個: if (!a.get(k).equals(b.get(i)))

我要去猜測a.get(K)獲取調用當k = = 6 ,但是a只有6(這意味着它的最高索引是5)。

我要去猜測,表達for (int k = (j + 1); k < (j + 6); k++)是罪魁禍首,我要去猜測的表達應該是for (int k = (j + 1); k < (j + 5); k++)

我只能猜測然而,因爲坦率地說(而不是試圖是粗魯的...只是說...),你的代碼是一團糟。更好的變量名稱,評論和更少的幻數使用會讓你走得更遠。

希望這會有所幫助。

相關問題