【问题标题】:Compressing a file压缩文件
【发布时间】:2016-01-25 21:09:04
【问题描述】:

我想完成一个可以接受文本文件并缩小大小的程序。到目前为止,它替换了所有出现的双字符,现在我想用“1”替换“ou”。

我尝试过使用 if 语句,但似乎效果不佳。

我的方法如下:

public String compressIt (String input)
    {
        int length = input.length(); // length of input
        int ix = 0;                  // actual index in input
        char c;                      // actual read character
        int cCounter;                // occurrence counter of actual character
        String ou = "ou";
        StringBuilder output =       // the output
                new StringBuilder(length);

        // loop over every character in input
        while(ix < length)
        {
            // read character at actual index then increments the index
            c = input.charAt(ix++);
            // we count one occurrence of this character here
            cCounter = 1;

            // while not reached end of line and next character
            // is the same as previously read
            while(ix < length && input.charAt(ix) == c)
            {
                // inc index means skip this character
                ix++;
                // and inc character occurence counter
                cCounter++;
            }


            if (input.charAt(ix) == 'o' && input.charAt(++ix) == 'u' && ix < length - 1)
            {
                output.append("1");
            }

            // if more than one character occurence is counted
            if(cCounter > 1)
            {
                // print the character count
                output.append(cCounter);
            }

            // print the actual character
            output.append(c);
        }
        // return the full compressed output
        return output.toString();
    }

我指的是这行代码。

if (input.charAt(ix) == 'o' && input.charAt(ix + 1) == 'u')
            {
                output.append("1");
            }

我想要做的:替换字符。我得到了一个包含“爱丽丝梦游仙境”的文本文件。当我遍历所有字符时看到一个“o”和一个“u”(如“你”),我想替换这些字符,使其看起来像:“Y1”。

问候

【问题讨论】:

  • 不应该是++ix吗?开头ix的值是多少,字符串有多长?
  • 首先,我认为您的意思是执行++ix 而不是ix++ - 就像您现在拥有的那样,您每次都检查相同的索引,并且仅在之后 i> 递增ix 的语句。但是一旦你解决了这个问题,如果input 是一个以o 结尾的字符串会发生什么?即if (charAt(ix) == 'o')为真,ix是字符串的最后一个索引?或者就此而言,如果 ix 本身超过最后一个索引,由于您可能处于循环的先前迭代,会发生什么?
  • 哦,我想你只想要ix + 1,而不是++ixix++(它们都修改了ix 的值)。
  • 我确实尝试了 ix + 1,这给了我同样的例外:-/
  • 查看更新版本。我插入了整个方法以避免误解

标签: java arrays string char stringbuilder


【解决方案1】:

所以很可能您正在尝试从 ix = 0 循环到字符串的长度。

首先我的猜测是你正在循环并包括 string.length()。哪个不起作用,charAt 是 0 索引又名 "abc" 有一个 charAt 0、1 和 2 但不是 3,这会给出您描述的错误。

您显示的所有行中的第二行使用input.charAt (ix++),它执行以下操作:获取位置 ix(旧值)的字符,然后将值 ix 更新为 ix + 1,如果您希望之前更新 ix周围的字符你必须写input.charAt(++ix)

还有一个 String.replace 函数,input.replace("abc", "def") 非常适合简单的替换,对于更复杂的替换,请考虑使用正则表达式。

【讨论】:

  • 感谢您的回复。是一个很棒的小课。在这种情况下,您将如何使用替换?查看更新后的帖子:)
【解决方案2】:

这与 charAt 方法无关。您需要更改 if 条件以将其运行到长度为 1。它在最后一种情况下失败,因为它正在输出数组。

for(int i=0; i<inputString.length() - 1; i++)
        {
            char temp = inputString.charAt(i);
            char blah = inputString.charAt(i+1);
            System.out.println("temp: "+ temp);
            System.out.println("blah: "+ blah);
        }

这对我有用!

【讨论】:

  • 为什么它与 charAt() 方法无关? :-) 只是想知道,所以我下次可以制作更好的标题。但请查看更新后的帖子。
  • @AlexanderFalk 两件事:当您的 while 条件运行到 ix
  • 喜欢在 if 语句中添加: && ix
猜你喜欢
  • 1970-01-01
  • 2010-09-05
  • 2019-02-24
  • 2010-12-14
  • 2013-03-09
  • 1970-01-01
  • 1970-01-01
  • 2015-05-04
相关资源
最近更新 更多