简体   繁体   English

为什么我的for循环不会读取字符串的最后一个字符?

[英]Why will my for loop not read my last char of the string?

I am trying to write a basic java program to compress a java string from an input; 我正在尝试编写一个基本的Java程序来压缩输入中的Java字符串; such as aabbccdddd , into a2b2c2d4 . 例如aabbccdddd ,转换为a2b2c2d4 The program does what I ask except it doesn't process the last char, I am getting an output of a2b2c2 instead of the a2b2c2d4 . 该程序按照我的要求做,除了不处理最后一个字符,我得到的是a2b2c2而不是a2b2c2d4的输出。 What am I doing wrong? 我究竟做错了什么?

for(x = 0, y = 1; x<input.length()-1; x++)
  {
      if (input.charAt(x) != input.charAt(x+1) && count == 1)
      {
        System.out.print(input.charAt(x));
        System.out.print(count);
      }

      else if (input.charAt(x) == input.charAt(x+y))
      {
        count++;          
      }

      else if (input.charAt(x) != input.charAt(x+1) && count >= 2)
      {
        System.out.print(input.charAt(x));
        System.out.print(count);
        count = 1;

      }
      else
      {
        System.out.println("fail");
      }
  }

You print the count when the next character is not same as the current one. 当下一个字符与当前字符不同时,您将打印计数。 There is no next character for the last character. 最后一个字符没有下一个字符。 That is why it is not displayed in the output. 这就是为什么它不显示在输出中的原因。

Approach 1 方法1

You should add following two lines after the loop: 您应在循环后添加以下两行:

System.out.print(input.charAt(input.length()-1));
System.out.println(count);

Approach 2 方法2

If you do not have problem with modifying the original input. 如果您对修改原始输入没有问题。 You can add a additional character in the end of the input. 您可以在输入末尾添加其他字符。 This additional character must be a character which will never appear in the original string. 此附加字符必须是永远不会出现在原始字符串中的字符。 Say it is # 说它是#

Do this before beginning of the loop: 在循环开始之前执行此操作:

input += "#"; 
for(...)

The loop is incorrect, you have "-1" after input.length(). 循环不正确,在input.length()后加“ -1”。 Try: 尝试:

    for(x = 0, y = 1; x<input.length(); x++) {
        // CODE HERE...
    }

Your for loop ends before you hit a condition that forces you to print out what's being buffered, ie. 您的for循环在遇到某种条件(迫使您打印出正在缓冲的内容)之前结束。 count of 4 for the current (last) character. 当前(最后一个)字符的计数为4。 You need to print out the last character and the current count after the loop. 您需要在循环后打印出最后一个字符和当前计数。

The following should do what you want 以下应该做你想做的

public static void main(String[] args) throws Exception {
    String input = "aabbccdddd";
    int count= 1;
    int x, y;
    for (x = 0, y = 1; x < input.length() - 1; x++) {
        char charAtX = input.charAt(x);
        char charAtXPlus1 = input.charAt(x + 1);
        if ( charAtX != charAtXPlus1 && count == 1) {
            System.out.print(input.charAt(x));
            System.out.print(count);
        }

        else if (charAtX == input.charAt(x + y)) {
            count++;
        }

        else if (charAtX != charAtXPlus1 && count >= 2) {
            System.out.print(input.charAt(x));
            System.out.print(count);
            count = 1;

        } else {
            System.out.println("fail");
        }
    }

    System.out.print(input.charAt(x));
    System.out.println(count);
}

You should learn how to use a proper debugger and use proper debugging techniques. 您应该学习如何使用适当的调试器并使用适当的调试技术。 For example, I've assigned the value returned by input.charAt(x) to a variable because we reuse that value in the various if-else conditions and because it's easier to see it in a debug window. 例如,我已经将input.charAt(x)返回的值分配给了一个变量,因为我们在各种if-else条件下重用了该值,并且因为在调试窗口中更容易看到它。

You are not able to get the desired result because of the condition 由于以下情况,您无法获得预期的结果

else if(input.charAt(x)!=input.charAt(x+1)&&count>=2)

that fails as there is not character at x+1 location. 失败,因为x + 1位置没有字符。 So, you could add another condition to check if it's the last character and then go ahead with your code 因此,您可以添加另一个条件来检查它是否为最后一个字符,然后继续执行代码

while (input.charAt(x) != input.length()-1)

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

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