2014-03-03 34 views
-2

我需要這個幫助。我試圖讓這個數組正常工作,但不知道我做錯了什麼。我是一個小白Java和真的需要一些幫助java.lang.ArrayIndexOutOfBoundsException:10 Array

private static int respondentID; 
    private static int count; 

    static void enterQuestions() { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 
    private String surveyName; 
    private boolean surveyStart = true; 
    private boolean surveyStop = true; 
    private String question [] = new String[10]; 
    private int responses[][]= new int[10][10]; 

    Scanner input = new Scanner(System.in); 
    // first overloaded constructor           
    public Phase2() {      

     this("Customer Survey"); 
     respondentID = 0; 
     count = 0;   
    } // end 

    // second overloaded constructor 
    public Phase2(String title) { 

     surveyName = title; 
     respondentID = 0; 
     count = 0; 
    } // end constructor Survey 

    // method to be called when a user starts filling out a survey (surveyStart will have been set to "true") 
    public int startSurveyCount(int ct) { // parameter for testing only 

     if(surveyStart) {    

      if(respondentID > 0) { 


       if(count >= respondentID) { 

        count++; 
       } else { 


        setCount(getRespondentID()); 
       } 
      } else { 
       //test 
       setCount(ct); 
       count = getCount(); 
       count++; 
       setCount(count - 1); 
      } 
     } 

     return count; 
    } // end method 

    // method to be called when a survey is successfully 
    public int generateRespondentID() { 
     if(surveyStop) { 
      // 
      count = getCount();      
      setRespondentID(count); 


     } else {     

      if(count < 2) { 
       count = 0;  
       respondentID = 0; 
      } else { 
       count--; 
       setCount(count); 
      } 
     } 

     return respondentID; 
    } // end method generateRespondentID 

    public void setRespondentID(int count) { 
     // count is the number of completed surveys. 
     respondentID = count; 
     respondentID++;  // and then incremented by 1. 
    } //end method 

    public int getRespondentID() { 
     return respondentID; 
    } // end method 

    public void setSurveyTitle(String title) { 

     surveyName = title; 
    } // end method 

    public String getSurveyTitle() { 
     return surveyName; 
    } // end method 

    public void setCount(int ct) { 

     count = ct; 
    } // end method 

    public int getCount() { 
     return count; 
    } // end method 

    public void setSurveyStart(boolean surveySt) { 

     surveyStart = surveySt; 
    } // end method 

    public boolean getSurveyStart() { 
     return surveyStart; 
    } // end method 

    public void setSurveySubmit(boolean surveySub) { 

     surveyStop = surveySub; 
    } // end method 

    public boolean getSurveySubmit() { 
     return surveyStop; 
    } // end method 

    public void logResponse(int respondentID, int questionNumber, int responseEntered) 
    { 
     responses[respondentID] [questionNumber-1] = responseEntered; 
     } 
    public void displaySurveyResults (int no) 
    { 
     for (int j=0; j<10; j++) 
     System.out.print("Question"+(no)+" : " + question[no-1]+"Reply"); 
     if (responses[respondentID][no] == 0) 
     { 
      System.out.print("NO"); 
    } 
    else 
    { 
     System.out.print("Yes"); 
    } 
    } 
    public void enterQuestion() 
    { 
    for (int i=0; i<10; i++) 
     { 
      System.out.println("Enter Question "+(i+1)+" : "); 
      question[i] = input.nextLine(); 
     } 
    } 
    public void displayQuestionStats(int no) 
    { 
     int answer; 
     System.out.print("Question"+(no)+" : "+question[no-1]+" (0-No/1-Yes) : "); 
     answer = input.nextInt(); 
     logResponse(respondentID, no, answer); 
    } 


} 

這是我的測試

public static void main(String[] args) { 




    Scanner input = new Scanner(System.in); 

     System.out.println("Below are the results of running the no–argument"); 

     // demonstrates the no–arg constructor 
     Phase2 noArgSurvey = new Phase2(); 
     System.out.printf("The no–argument survey values are:\ntitle: %s\n" 
      + "initial value of respondentID: %d\ncount: %d\n", 
      noArgSurvey.getSurveyTitle(), noArgSurvey.getRespondentID(), 
      noArgSurvey.getCount()); 

     // demonstrates the constructor with a title argument (for user input of survey title) 
     System.out.println("\nPlease enter a name for the survey"); 
     String inputTitle = input.nextLine(); 
     System.out.println(); // inserts a blank line 
     Phase2 titleArgConst = new Phase2(inputTitle); 
     System.out.printf("Survey Name is: %s\n" 
      + "initial values of:\nrespondentID: %d\ncount: %d\n\n", 
      titleArgConst.getSurveyTitle(), titleArgConst.getRespondentID(), 
      titleArgConst.getCount()); 

     //respondent id test 
     System.out.println("This will test the generateRespondentID method.\n\n" 
      + "Enter the number of surveys that have been taken"); 
     int testInt = input.nextInt(); 

     // values for respondentID and count after 1 survey has been successfully submitted 
     System.out.println("\nAssuming " + testInt + " surveys submitted"); 
     Phase2 oneDone = new Phase2(); 
     oneDone.startSurveyCount(testInt); 
     oneDone.generateRespondentID(); 
     System.out.printf("The Respondent ID is: %d\ncount: %d\n\n", 
      oneDone.getRespondentID(), oneDone.getCount());  

     noArgSurvey.enterQuestion(); 

     for(int i = 1; i <= 10; i++) 
     { 
      noArgSurvey.displayQuestionStats(i); 
     } 

     //Display The Inputs Entered by User 
     System.out.println("Result for Survey with Title \""+titleArgConst.getSurveyTitle()+"\" :"); 
     for(int i=1; i<11; i++) 
     { 
      noArgSurvey.displaySurveyResults(i); 
     } 

    } // end main method 
} // end class SurveyTest 
+1

數組是基於0索引的。你可以在'displayQuestionStats'中處理,而不是'logResponse'中。 –

+0

當你問一個關於例外的問題時,你應該指出引發它的那一行。這是IDE會在堆棧跟蹤中告訴您的信息。 – Radiodef

回答

1

改變的環路條件

for(int i = 1; i < 10; i++) 
0

的Java如下從零開始的索引。所以在你的情況下,questionresponses數組的大小爲10,這意味着它將從0 to 9迭代使用for(int i = 1; i < 10; i++)來代替迭代。或使用arrayName.lengthlength是由JVM提供的變量,它給出了在運行時數組的size

0

AS Vishrant提到* Java遵循零索引。所以你的情況的問題和對策數組大小爲10,這意味着它會遍歷從0到9 *

在測試儀類

,您試圖訪問環路10指數在2個地方

for(int i = 1; i <= 10; i++)   (1) 
    { 
     noArgSurvey.displayQuestionStats(i); 
    } 

for(int i=1; i<11; i++)     (2) 
    { 
     noArgSurvey.displaySurveyResults(i); 
    } 

你應該寫

for(int i = 0; i < 10; i++)   (1) 
    { 
     noArgSurvey.displayQuestionStats(i); 
    } 

for(int i=0; i<10; i++)    (2) 
    { 
     noArgSurvey.displaySurveyResults(i); 
    } 

編輯加成

public void displaySurveyResults (int no) 
{ 
    for (int j=0; j<10; j++) 
    System.out.print("Question"+(no)+" : " + question[no]+"Reply"); <<<--------- change [no-1] to [no] 
    if (responses[respondentID][no] == 0) 
    { 
     System.out.print("NO"); 

}

+0

然後在主線上的第168行和測試儀上的第55行上輸出錯誤 – user3372117

+0

我們怎麼知道哪一行是168號? System.out.print(「Question」+(no)+「:」+ question [no-1] +「(0-No/1-Yes):」);您應該顯示一些堆棧跟蹤信息 –

+0

是行168 – user3372117

相關問題