Advertisement
Kwintendr

Untitled

Dec 18th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.72 KB | None | 0 0
  1.  * @param args
  2.      */
  3.     public static void main(String[] args) {
  4.         System.out.println(increment("1239"));
  5.  
  6.     }
  7.  
  8.     public static String increment(String s) {
  9.         StringBuffer p = new StringBuffer(s);
  10.  
  11.        
  12.  
  13.         if (p.charAt(p.length() - 1) != 9) {
  14.             int k = p.charAt(p.length() - 1);
  15.             p.setCharAt(p.length() - 1, (char) (k + 1));
  16.         } else {
  17.             int i = 0;
  18.             while (p.charAt(p.length() - 1 - i) == '9') {
  19.                 p.setCharAt(p.length() - 1 - i, '0');
  20.                 i = i + 1;
  21.  
  22.             }
  23.             if ( i+1!=p.length()) {
  24.                 int k = p.charAt(p.length() - 1 - i);
  25.                 p.setCharAt(p.length() - 1 - i, (char) (k + 1));
  26.             } else {
  27.                 p.setCharAt(p.length() - i - 1, '0');
  28.                 p.insert(0, '1');
  29.  
  30.             }
  31.  
  32.         }
  33.        
  34.         return p.toString();
  35.     }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement