剪切String的特定部分

时间:2017-11-26 23:41:27

标签: java string substring handle cut

首先,我的英语不好... 我有一个问题,我现在试了几个小时。希望你们能帮助我。

情况: 我们得到了一个包含24个数字的String。我需要切掉前9个数字并用模97计算它然后我需要将结果挂到其余的长度。最后,我需要循环这种情况。

示例输出(伪):

err

这是我的代码:

Start: 123456780000123456131400
---------------------------------------------------------------------
Cut 9 of them: 123456780
Calculate: 123456780 % 97 = 30
The rest of the length: 000123456131400
Add result in front of the rest of the length: 30000123456131400
---------------------------------------------------------------------
Cut 9 of them: 300001234
Calculate: 300001234 % 97 = 22
The rest of the length: 56131400
Add result in front of the rest of the length: 2256131400
---------------------------------------------------------------------
Cut 9 of them: 225613140
Calculate: 225613140 % 97 = 64
The rest of the length: 0
Add result in front of the rest of the length: 640
---------------------------------------------------------------------
Calculate:  640 % 97 = 58

输出:

        String total = "123456780000123456131400";
        String ninecharacters = total.substring(0, 9);
        int newTotal = Integer.parseInt(ninecharacters);
        int newSum = 0;

        while(newTotal > 100) {

            if (total.length() > 9) 
                ninecharacters = total.substring(0, 9);
              else 
                ninecharacters = total.substring(0, total.length());


            newTotal = Integer.parseInt(ninecharacters);
            newSum = newTotal % 97;
            String summedUp = Integer.toString(newSum);
            String newString = summedUp + total.substring(total.indexOf(ninecharacters) + ninecharacters.length());
            total = newString;
            System.out.println(newSum);


        } 

一切都好,但为什么我得到2倍58?我该怎么处理?

0 个答案:

没有答案