java数字字符串自增

 要求:

输入"0022"输出"0023",输入非纯数字字符串提示.

public class test1 {

    public static void  addValue(String str){
        int index = str.lastIndexOf("0");
        int temp =0;
        
        //以0开头的
        if (str.startsWith("0")&&index!=-1){
            temp=index+1;
        }
        Integer integer = null;
        try {
            integer = Integer.valueOf(str);
        } catch (NumberFormatException e) {
            System.out.println("输入的字符串不是纯数字字符串...");
            return;
        }
        integer++;
        String newNum = integer.toString();

        if (newNum.length()>str.length()-temp){
            temp--;
        }
        StringBuffer sb = new StringBuffer();
        if (temp!=0){
            for (int i =0 ;i

 

你可能感兴趣的:(JavaSE)