Check out the for
loop part of your code:
for(int i = 0; i <= line.length(); i++) {
It should be changed to this:
for(int i = 0; i < line.length(); i++) {
Notice how <=
was changed to <
:
for(int i = 0; i <= line.length(); i++) {
//vs:
for(int i = 0; i < line.length(); i++) {
For pretty much all coding, including java
, counting starts at 0. So where you would normally say 5, in coding you would say 4.
Let's say the length of the array is 3
, meaning it contains 0
, 1
, and 2
. So, when doing this:
for(int i = 0; i <= line.length(); i++) {
//length of the line is 3, so:
for(int i = 0; i <= 3; i++) {
now, it will loop threw the numbers 0
, 1
, 2
, and 3
. Yet, the line is only contains only 0
, 1
, and 2
. So, if we were to do this:
for(int i = 0; i < line.length(); i++) {
//length of the line is 3, so:
for(int i = 0; i < 3; i++) {
Using this new code, it will loop threw 0
, 1
, and 2
, the same length of our line, 0
, 1
, and 2
.