Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

drew s
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
drew s
Python Development Techdegree Graduate 19,491 Points

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9

import java.io.Console;

public class practice {

public static void main(String[] args){

  Console console = System.console();


  String[] names = {"Felix", "Andrew", "John", "Jade", "Kelvin", "Mark", "Kevin", "Mike", "Sandra"};

  int prizesGivenAway = 0;
  int drawingNumber = 0;

  while(prizesGivenAway < 8){
    String winner = names[drawingNumber];
    drawingNumber++;
    console.printf("Is %s present? ", winner);
    String isHere = console.readLine();
    if(isHere.equalsIgnoreCase("no")){
      continue;
    }

    String size = console.readLine("What is your shirt size? ");
    prizesGivenAway++;
  }

  }

}

drew s
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
drew s
Python Development Techdegree Graduate 19,491 Points

counting array elements using index, If I input Yes 7 times NO ERROR, input all yes NO ERROR, but if I input 2 no's I am getting this message "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9". I know this is happening because of my parameter prizesGivenAway < 8. Can someone explain this to me on what's happening?

2 Answers

Tabatha Trahan
Tabatha Trahan
21,422 Points

The index is using drawingNumber to pull an item from your array, that has 9 items in it, and drawingNumber is incremented each time through the loop no matter what the answer is to "is present". If you answer no at all, you will have prizesGivenAway be less than drawingNumber, so drawingNumber will reach the end of the array before the loop condition will become false (prizesGivenAway). If you want to loop until prizesGivenAway is equal to the number of elements in the array, you could do something like this with your while loop:

while(prizesGivenAway < names.length){
    String winner = names[drawingNumber];
    drawingNumber++;
    if(drawingNumber == names.length ){
        drawingNumber = 0;
    }
    console.printf("Is %s present? ", winner);
    String isHere = console.readLine();
    if(isHere.equalsIgnoreCase("no")){
      continue;
    }

    String size = console.readLine("What is your shirt size? ");
    prizesGivenAway++;
  }

The if statement checks to make sure you don't go over the number of elements in the array. If is, then it is reset to 0 and the loop continues through the array again.