Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- * @param args
- */
- public static void main(String[] args) {
- System.out.println(increment("1239"));
- }
- public static String increment(String s) {
- StringBuffer p = new StringBuffer(s);
- if (p.charAt(p.length() - 1) != 9) {
- int k = p.charAt(p.length() - 1);
- p.setCharAt(p.length() - 1, (char) (k + 1));
- } else {
- int i = 0;
- while (p.charAt(p.length() - 1 - i) == '9') {
- p.setCharAt(p.length() - 1 - i, '0');
- i = i + 1;
- }
- if ( i+1!=p.length()) {
- int k = p.charAt(p.length() - 1 - i);
- p.setCharAt(p.length() - 1 - i, (char) (k + 1));
- } else {
- p.setCharAt(p.length() - i - 1, '0');
- p.insert(0, '1');
- }
- }
- return p.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement