1

This is the code I have right now

for (int i = 0; i <= listOfPeople.length; i++){
    String name = scnr.nextLine();
    System.out.println("Person " + (i + 1) +  ": ");
    listOfPeople[i] = name;
}

List of people is a properly declared list of Strings with the length of a value the user sends in. The error that is happening is that when I run the program, I get this:

Person 1: 

Jordan

Person 2: 

Jordan

Person 3:

Jordan

Person 4: 

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at RGG.main(RGG.java:20)

I am not quite sure what is wrong, but I have tried removing the = in the for loop declaration, then I get this output:

Person 1: 

Jordan

Person 2: 

Jordan

Person 3: 

After the third prompt, the code moves on and I cant type in anything there. Does anyone know what might be happening? Thanks in advance!

1
  • As I said in the question, " I have tried removing the = in the for loop declaration", it still fails, see above issues Commented Dec 20, 2013 at 1:07

4 Answers 4

2

Remove the = in this expression i <= listOfPeople.length;. Its causing you to access an element of the array that does not exist.

for (int i = 0; i < listOfPeople.length; i++){
        String name = scnr.nextLine();
        System.out.println("Person " + (i) +  ": ");
        listOfPeople[i] = name;
}

Full Example:

public class PersonArrayTest {

    public static void main(String[] args) {
        String[] listOfPeople = new String[5];
        assign(listOfPeople);
        System.out.println(Arrays.toString(listOfPeople));
    }

    public static void assign(String[] listOfPeople) {

        Scanner scnr = new Scanner(System.in);
        for (int i = 0; i < listOfPeople.length; i++) {
            String name = scnr.nextLine();
            System.out.println("Person " + (i) + ": ");
            listOfPeople[i] = name;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

With this line

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

You are advancing one beyond the end of the array, length 3, which has valid indices 0-2. 3 is an invalid index.

When you remove the =, you get the corrected version:

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

which stops after the 2 iteration, which is the end of the array, before you run off the end of the array.

Comments

1

change this the <= sign in forloop

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

Comments

0

I am make an educated guess that you are using Scanner#nextInt to get the input for the length of the array. Something along the lines of this:

String[] listOfPeople = new String[scnr.nextInt()];

I've garnered this because your loop code looks like this:

take input for i == 0
print prompt #1
take input for i == 1
print prompt #2
take input for i == 2
print prompt #3

But your output shows this:

print prompt #1
take input for i == 1
print prompt #2
take input for i == 2
print prompt #3

So what actually must be happening is this:

silently advance past whatever scnr is still retaining for i == 0
print prompt #1
take input for i == 1
print prompt #2
take input for i == 2
print prompt #3

nextInt orphans a new line character. (So will any calls to next_ besides nextLine.) That's why your first input is skipped. Calling scnr.nextLine in the first iteration of the loop just advances the Scanner past the last line.

Change the loop to this:

// skip the last new line
scnr.nextLine();

// < not <=
for (int i = 0; i < listOfPeople.length; i++) {

    // prompt before input
    System.out.println("Person " + (i + 1) +  ": ");

    // you don't need that extra String
    listOfPeople[i] = scnr.nextLine();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.