Your problem is that when you call checkNestedParenFront(check)
, you're returning the count in check
of that last parenthesis, which is not the same as the count in line
, because check
starts partway through line
.
Then when you call line.substring(posComma, i)
, you have i
less than posComma
, and that causes the exception.
I think you mean to have line.substring(posComma, posComma + i + 1)
- but I'm not quite sure about the +1
, since it's not clear exactly what you're trying to achieve.