Sidar Yaksan Sidar Yaksan - 6 months ago 9
Java Question

Java for loop is not being executed

I have written the following

for
loop, but it never gets executed:

System.out.println("1");

Dog dogObjects[] = new Dog[5];
System.out.println("2");

for (int i =1; i >= dogObjects.length; i++){
System.out.println("3");
dogObjects[i].userInput();
}

System.out.println("6");


The output is:

1
2
6


Can someone tell me if I am braindead, or if it's the computer please? I already have a headache already because of that.

Answer

I think you want something like this.

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

The first element of dogObjects is at index 0, the last one is at (dogObjects.length - 1).

Comments