Jordan McGowan
Jordan McGowan

Reputation: 197

Simple for loop issue with arrays

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!

Upvotes: 1

Views: 91

Answers (4)

Radiodef
Radiodef

Reputation: 37875

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();
}

Upvotes: 0

Kevin Bowersox
Kevin Bowersox

Reputation: 94499

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;
        }
    }
}

Upvotes: 2

Nambi
Nambi

Reputation: 12042

change this the <= sign in forloop

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

Upvotes: 1

rgettman
rgettman

Reputation: 178333

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.

Upvotes: 2

Related Questions