java - 展开压缩字符串

标签 java parseint charat

我正在尝试采用与此类似的字符串:3A5o2n4t 并将其扩展回如下字符串:AAAooooonntttt(字母前面的数字是字母重复的次数) 我试图使用 Integer.parseInt() 来获取字母前面的数字,但它获取了所有数字。有没有办法一次获取一个号码?另外,问题解决后我的代码看起来还好吗?还是我还缺少一点?

public String runLengthDecoding(String str3) {
          String convert = "";
          int number = 0;
          if (! str3.isEmpty()) {
             convert = str3.charAt(0) + "";
          }
          for (int i = 0; i <= str3.length() - 1; i++) { 
             if (Character.isDigit(str3.charAt(i))) { //true or false, the current character is a digit
                String temp = "" + str3.charAt(i); //if true, make that character a string
                number = Integer.parseInt(temp); /*now store that character as a number (but I only want the current
                                                   number, not all the numbers in the string*/
                System.out.println(number); /*Testing to see what number is, which is where I found it was 
                                               storing all the numbers */
                String temp2 = str3.charAt(i + 1) + ""; //Its supposed to start making a string that prints the character in front of it
                convert = temp2.repeat(number); //it will print the character however many times that number was stored as
             }

       }
       return convert;
       }

而且我还没有学会如何使用数组,这就是我不使用数组的原因。

最佳答案

编辑为:
- 容纳长度大于 1 的字符串。示​​例:10AA
- 容纳以字符串开头的输入。示例:A5o

要解决这个问题,您必须获得所有同时出现的数字,例如,如果您有“55s”,则必须获得“55”,这就是您的代码不正确的原因,因为如果您在看到数字时解析Int,那么它就会立即解析为5,但实际数字是55,因此应该先累加同时出现的数字,遇到第一个非数字时才进行parseInt。

详细引用代码和注释:

public class Main {
    public static void main(String[] args) {
        System.out.println("Input: 3A5o2n4t => Output : " + runLengthDecoding("3A5o2n4t"));
        System.out.println("Input: 3AA5o2n4t => Output : " + runLengthDecoding("3AA5o2n4t"));
        System.out.println("Input: 10A5o2n4t => Output : " + runLengthDecoding("10A5o2n4t"));
        System.out.println("Input: 10AA5o2n4t => Output : " + runLengthDecoding("10AA5o2n4t"));
        System.out.println("Input: A5o => Output : " + runLengthDecoding("A5o"));
        System.out.println("Input: AB5o => Output : " + runLengthDecoding("AB5o"));
    }

    public static String runLengthDecoding(String str3) {
        String convert = "";
        int number = 0;
        String numberString = "";
        String toBeRepeatedString = "";
        boolean flag = false;
        for (int i = 0; i <= str3.length() - 1; i++) {
            char currentChar = str3.charAt(i);
            if (Character.isDigit(currentChar)) { // true or false, the current character is a digit
                numberString = numberString + currentChar; // store the possible integer
            } else {
                if (i + 1 < str3.length()) {
                    char nextChar = str3.charAt(i + 1); // check if the next char is a digit
                    if (!Character.isDigit(nextChar)) { // if not a digit then append toBeRepeatedString

                        if (i == 0 || i + 1 >= str3.length()) {
                            flag = true;
                        } else {
                            toBeRepeatedString += nextChar;
                            flag = false;
                        }
                    } else {
                        flag = true;
                    }
                }

                if (flag) {
                    toBeRepeatedString += currentChar;

                    // This will accomodate inputs "A3B";
                    if (!numberString.isEmpty()) {
                        number = Integer.parseInt(numberString); // parse the number of repeats
                    } else {
                        number = 1;
                    }

                    numberString = ""; // reset number

                    String temp2 = "";

                    // Repeat the currentChar
                    for (int j = 0; j < number; j++) {
                        temp2 += toBeRepeatedString;
                    }

                    convert = convert + temp2; // store it to the result
                    toBeRepeatedString = ""; // reset toBeRepeatedString
                }

            }

        }
        return convert;
    }

}

结果:

Input: 3A5o2n4t => Output : AAAooooonntttt
Input: 3AA5o2n4t => Output : AAAAAAooooonntttt
Input: 10A5o2n4t => Output : AAAAAAAAAAooooonntttt
Input: 10AA5o2n4t => Output : AAAAAAAAAAAAAAAAAAAAooooonntttt
Input: A5o => Output : Aooooo
Input: AB5o => Output : ABooooo

关于java - 展开压缩字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55035500/

相关文章:

java - 在 tomcat 5.5 上部署应用程序时出现以下错误

java - 需要帮助将字符串拆分为两个单独的整数进行处理

java - 通过使用 for 循环使用 java textfield.setText() 输出字符串

java - 从最后一个字符开始向后迭代 3

java - 如何使用 EXT-GWT 组合框

java - 如何通过邮件启动 AWS SWF 工作流程?

javascript - 在 JavaScript 中,我发现 parseInt ("fffffffffffff2",16) 有时会出现错误的结果

Java 日志文件读取器,charAt() 字符串索引超出范围

java - 获取单词的第一个字母并将其放在最后

java - ARCore – 渲染距离相机 200 米的物体