简体   繁体   English

为什么最后一个字符不出现?

[英]Why does the last char doesn't appear?

I want to encode a String like ...----....--.-.-. 我想编码一个像...----....--.-.-.这样的字符串...----....--.-.-. to .3-4.4-2.1-1.1-1.1 - showing, that there are three dots first (.3), then four hyphens (-4) next and so on. .3-4.4-2.1-1.1-1.1显示,先是三个点(.3),然后是四个连字符(-4),依此类推。

I nearly made it, but there's a mistake anywhere... 我差点就做到了,但是任何地方都有错误...

static String pointsAndLines(String s){

    StringBuffer encodedString = new StringBuffer();

    int counter = 1;
    char currentChar = s.charAt(0);

    for(int i=1; i<s.length(); i++){
        if(s.charAt(i) != currentChar){
            encodedString.append(String.valueOf(currentChar) + counter);
            currentChar = s.charAt(i);
            counter = 1;
        } else counter++;
    }

    return encodedString.toString();

}

But the output is this: 但是输出是这样的:

.3-4.4-2.1-1.1-1

The last .1 is missing - but why? 最后一个.1不见了-但是为什么呢?

The loop is terminated once you hit the end of the string, and the last group isn't appended to the buffer. 一旦您到达字符串的末尾,循环就会终止,并且最后一组不会追加到缓冲区中。 You could just explicitly append it: 您可以明确地附加它:

for(int i=1; i<s.length(); i++){
    if(s.charAt(i) != currentChar){
        encodedString.append(String.valueOf(currentChar) + counter);
        currentChar = s.charAt(i);
        counter = 1;
    } else counter++;
}
encodedString.append(String.valueOf(currentChar) + counter); // Here

In your code: 在您的代码中:

encodedString.append(String.valueOf(currentChar) + counter); encodeString.append(String.valueOf(currentChar)+计数器);

the last .1 is not appended to the StringBuffer because you are taking the value of currentchar which holds the previous value that is - on index 16 and not . 最后一个.1不会附加到StringBuffer上,因为您要获取currentchar的值,该值保留了在索引16上而不是在-上的先前值。 on index 17 when the loop has reached its end that is value of i is equal to String s length minus 1. 在索引17上,当循环结束时,i的值等于String的长度减去1。

To get the output .3-4.4-2.1-1.1-1.1 you can use two for loops as follows: 要获得输出.3-4.4-2.1-1.1-1.1,可以使用两个for循环,如下所示:

 public class StackOverFlowStringBuffer {

    public static void main(String[] args) {

        System.out.println(pointsAndLines("...----....--.-.-."));
    }

    static String pointsAndLines(String s) {

        StringBuffer encodedString = new StringBuffer();

        int counter = 1;
        char currentChar;

        for (int i = 0; i < s.length(); i++) {
            currentChar = s.charAt(i);
            if (i < s.length() - 1) {
                for (int j = i + 1; j < s.length(); j++) {
                    if (s.charAt(j) != currentChar) {
                        encodedString.append(String.valueOf(currentChar) + counter);
                        counter = 1;
                        i = j - 1;
                        break;
                    } else {
                        counter++;
                    }
                }
            } else {
                encodedString.append(String.valueOf(currentChar) + counter);
            }
        }

        return encodedString.toString();

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM