By calling lines.next() you’re mutating the iterator while iterating. It’s not clear what you’re really trying to do, but simply replacing that by continue might be what you want. You should look into filter though, which is a more elegant and effective way to achieve that.