Check for isEmpty before calling charAt(0)