2017-09-12 2 views
0

各選択肢に3つのオプションがある場合は、50個の選択肢の設定に追いつくためにカウント配列を集計しようとしています。カウント配列には、インストラクタ(3 x 50 = 150)あたり150個の要素が必要です。しかし、私は行55(index = thisChoice.get(i))でIndexOutofBounds例外を取得し続けます。私は、コードの残りの部分は私のインストラクターから来て、おそらく正しいですので、私は配列をインスタンス化しますが、引き続きIndexOutOfBoundsを取得する例外

line 50: int[] count = new int[students.get(0).getChoices().size()*3] 

で私のカウント配列をインスタンス化しています、それはどのように(またはどこ?)とは何かを持っている必要があることを考えています。それを範囲外に送る可能性のあるアイデアはありますか?

public class P1Driver { 

public static void main(String[] args) throws IOException{ 

    ArrayList<Students> students = new ArrayList<Students>(); 
    ArrayList<String> choices = new ArrayList<String>(); 
    Scanner scan1 = new Scanner(new File("Choices.txt")); 
    Scanner scan2 = new Scanner(new File("EitherOr.csv")); 

    // Scan the first file. 
    int choicesIndex = 0; 
    while(scan1.hasNextLine()){ 
     String line = scan1.nextLine(); 
     choices.add(line); 
     choicesIndex++; 
    } 
    scan1.close(); 

    // Scan the second file. 
    int studentIndex = 0; 
    while(scan2.hasNextLine()){ 
     String line = scan2.nextLine(); 
     String [] splits = line.split(","); 

     students.add(new Students(splits[0])); 

     for(int i = 1; i < splits.length; i++){ 
      students.get(studentIndex).addChoices(Integer.parseInt(splits[i])); 
     } 
     studentIndex++; 
    } 
    scan2.close(); 

    // Instantiate and add to the count array. 
    int index, countIndex; 
    ArrayList<Integer> thisChoice; 
    int[] count = new int[students.get(0).getChoices().size()*3]; 
    for(int i = 0; i < students.size(); i++){ 
     countIndex = 1; 
     thisChoice = students.get(i).getChoices(); 
     for(int j = 0; j < thisChoice.size(); j++){ 
      index = thisChoice.get(i); 
      count[countIndex + index] = count[countIndex + index] + 1; 
      countIndex+=3; 
     } 
    } 

    // Display data. 
    countIndex = 1; 
    for(int i = 0; i < choices.size(); i+=2){ 
     System.out.println(choices.get(i) + count[countIndex] + choices.get(i+1) + count[countIndex+1] + " Invalid: " + count[countIndex-1]); 
     countIndex+=3; 
    } 
+0

what's line 55? –

+0

オリジナルの投稿に追加しました。私はそれを残して申し訳ありません! –

+0

例外を与える行を注意深く見てください。あなたはタイプミスをしました。 – ajb

答えて

1

HIそれはjの代わりiである必要があり、第二ネストされたループを確認してください。 また、そのループでint jを使用していません。

for (int i = 0; i < students.size(); i++) { 
     countIndex = 1; 
     thisChoice = students.get(i).getChoices(); 
     for (int j = 0; j < thisChoice.size(); j++) { 
      index = thisChoice.get(j); 
      count[countIndex + index] = count[countIndex + index] + 1; 
      countIndex += 3; 
     } 
    } 
関連する問題