Array error with string

I have an issue with string array where's the program takes the use names the put it in the screen. I did some coding and creating 2D games and android app but the fact I never used array for saving scores or something and now I'm stuck and need to learn it and code below think of it as we are putting degrees for collage students put the error that the array is making an error and I can't figure it out why the full code below:

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Chatlength = new String[10];

    for(i =0; i <= Chatlength.length ; i++){
        Scanner s = new Scanner(System.in);
        String ss = s.nextLine();
        Chatlength[i] = ss;


        }
    while(true){
    if(i > Chatlength.length){
            int ints = 0;
        while(ints <10){
            System.out.println("Name "+ints+": "+Chatlength[ints]);
            ints++;
        }
    }
    }

It gives me and error with Chatlength[i] = ss;.

Jon Skeet
people
quotationmark

I'm guessing you're getting an ArrayIndexOutOfBoundsException due to your loop bounds:

for(i =0; i <= Chatlength.length ; i++){

That should be:

for (int i = 0; i < Chatlength.length; i++) {

... using a local variable declaration instead of the static variable which I assume you've declared in code that you haven't shown us.

An array of length 10 has valid indexes of 0 to 9 inclusive. It's very rare that you actually want <= for a loop index variable when you're trying to iterate over a collection. (All the standard collections in Java are 0-based, so you pretty much always want to have an exclusive upper bound.)

Additionally, I strongly suggest that you start following Java naming conventions.

people

See more on this question at Stackoverflow