Method 1: operate StringBuffer.
In strb.append(ch); add:strb . delete(strb . length()- 1,strb . length());
Method 2: the operation string intercepts the string.
Add s = s.substring (0, s.length ()-1) after s;
Both ways can be used, of course, there are other ways, I hope I can help you!