result = result.substring(0,result.length()-1); is used just to remove the last trailing space.