Okay -- for the life of me I can't seem to figure this out (which probably means that it's simple and I'm missing the obvious). It's been a long time since I've been here, and I'm getting back into
Java. I'm just putting together a little 'horse racing' program while I'm learning Java, and I have a class called Race that creates an array list of thorougbred horses called field, and asks the user to enter then names of the each horse in the field, with a maximum of 14 horses.
The problem occurs with the current code that I have:
And here's the output:
First, enter the names of the horses in the field, up to a maximum of 14 horses; enter 'quit' to stop entries.
current size of field: 0
Enter the name of horse number 1: horse 1
The name of the horse at index 0:horse 1
current size of field: 1
Enter the name of horse number 2: horse 2
The name of the horse at index 1:horse 2
current size of field: 2
Enter the name of horse number 3: horse 3
The name of the horse at index 2:horse 3
current size of field: 3
Enter the name of horse number 4: horse 4
The name of the horse at index 3:horse 4
current size of field: 4
Enter the name of horse number 5: horse 5
The name of the horse at index 4:horse 5
current size of field: 5
Enter the name of horse number 6: horse 6
The name of the horse at index 5:horse 6
current size of field: 6
Enter the name of horse number 7: horse 7
The name of the horse at index 6:horse 7
current size of field: 7
Enter the name of horse number 8: horse 8
The name of the horse at index 7:horse 8
current size of field: 8
Enter the name of horse number 9: horse 9
The name of the horse at index 8:horse 9
current size of field: 9
Enter the name of horse number 10: horse 10
The name of the horse at index 9:horse 10
current size of field: 10
Enter the name of horse number 11: horse 11
The name of the horse at index 10:horse 11
current size of field: 11
Enter the name of horse number 12: horse 12
The name of the horse at index 11:horse 12
current size of field: 12
Enter the name of horse number 13: horse 13
The name of the horse at index 12:horse 13
current size of field: 13
Enter the name of horse number 14: horse 14
The name of the horse at index 13:horse 14
current size of field: 14
Enter the name of horse number 15: horse 15
The name of the horse at index 14:horse 15
There are 15 horses in this race.
Name of horse at index 0:horse 1
Name of horse at index 1:horse 2
Name of horse at index 2:horse 3
Name of horse at index 3:horse 4
Name of horse at index 4:horse 5
Name of horse at index 5:horse 6
Name of horse at index 6:horse 7
Name of horse at index 7:horse 8
Name of horse at index 8:horse 9
Name of horse at index 9:horse 10
Name of horse at index 10:horse 11
Name of horse at index 11:horse 12
Name of horse at index 12:horse 13
Name of horse at index 13:horse 14
Name of horse at index 14:horse 15
the statement of the index position and current size was for me, so I could see what was going on.
What I don't understand is the while loop. not that the class doesn't compile and run (you can see that it does), it's the output. Why does the <= sign allow one more entry and increase the size of the field to 15?
Less than or equal to 14 should give a maximum field size of 14, right, With the starting object at index position at zero and going up to 13, for a total size of 14 thoroughbred objects
if I just use
or a for loop, then the output is fine; it allows a max of 14 entries and prints the results. I thought it had something to do with the size being zero based, but that doesn't seem to matter -- unless it does matter and I'm missing it.
Can anyone tell me why the comparison I'm using produces this output? a field of 14 horses shouldn't matter whether it's zero or 1 based, as long as the size of the field is 14, so why the extra entry with this while condition?