Java 压缩文件

Java 压缩文件,java,arrays,string,char,stringbuilder,Java,Arrays,String,Char,Stringbuilder,我想完成一个程序,可以采取一个文本文件,使大小更小。到目前为止,它替换了所有出现的双字符,现在我想用“1”替换“ou” 我尝试过使用if语句,但似乎效果不太好 我的方法如下: public String compressIt (String input) { int length = input.length(); // length of input int ix = 0; // actual index in inpu

我想完成一个程序,可以采取一个文本文件,使大小更小。到目前为止,它替换了所有出现的双字符,现在我想用“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();
    }
我想做的是:替换字符。我得到了一个包含“爱丽丝梦游仙境”的文本文件。当我在所有字符中循环看到一个“o”和一个“u”(如“You”)时,我想替换这些字符,使其看起来像:“Y1”


考虑到

,因此您很可能正在尝试从ix=0循环到字符串的长度

首先,我猜测您正在循环并包括string.length()。这不起作用,charAt是0 “abc”有一个字符0、1和2,但不是给出您描述的错误的3

第二行使用了
input.charAt(ix++)
,它执行以下操作:在位置ix处获取字符(旧值),然后将值ix更新为ix+1,如果希望在周围字符之前更新ix,则必须编写
input.charAt(++ix)


P>第三。有一个字符串。替换函数,输入。替换(“ABC”,“DEF”)对于简单替换是很有用的,对于更复杂的替换,考虑使用正则表达式。

< P>这与CARAT方法无关。你需要改变你的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);
        }

用于(int i=0;我不应该是
++ix
?开始时
ix
的值是多少,字符串有多长?首先,我想你的意思是做
++ix
而不是
ix++
——按照现在的方式,你每次都在检查相同的索引,而且只有在语句增加
ix
之后。但是一旦你解决了这个问题,如果
input
是一个以
o
结尾的字符串,会发生什么情况?也就是说,
if(charAt(ix)='o')
是正确的,并且
ix
是字符串的最后一个索引?或者,就这一点而言,如果
ix
本身超过了最后一个索引,那么会发生什么情况,这是由于您大概所处的循环的上一次迭代造成的?哦,我想您只是想要
ix+1
,而不是
++ix
ix++
(两者都修改了
ix
)的值。我确实尝试了ix+1,这给了我同样的例外:-/检查更新的版本。我插入了整个方法以避免误解谢谢你的回答。这是一个很好的小教训。在这种情况下,你会如何使用替换?检查更新的帖子:)为什么它与charAt()方法无关?:-)我只是想知道,这样我下次可以制作更好的标题。但是请查看更新后的帖子。@AlexanderFalk有两件事:当while条件运行到ixfor(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); }