instead of using input.substring(i, i +1); I think it would be simpler to just use input.charAt(i);