硬编码字符串有效,输入字符串无效

发布于 2024-11-10 07:05:58 字数 2306 浏览 2 评论 0原文

简而言之,这就是我想要做的:

我想读取一个文件并检测符号后面的字符是数字还是单词。如果是数字,我想删除它前面的符号,将数字翻译成二进制并替换到文件中。如果是一个单词,我想首先将字符设置为数字16,但是,如果使用另一个单词,我想将1添加到原始数字并继续循环,直到到达输入的末尾。

当我硬编码我想要输入和读取的字符串时:

String input = "@5\n@word1\n@word2\n@word1\n@6";
String[] lines = input.split("\n"); // divide up the array

或者:

@5
@word1
@word2
@word1
@6

然后它输出我想要输出的内容:

101
10000
10001
10000
110

但是当我输入anyLines[i](一个包含文件信息的数组,如果选择另一个文件,该文件信息可以更改) ):

String input = anyLines[i];
String[] lines = input.split("\n");

对于相同的数据,突然给出了错误的输出:

101
10000
10000 <-- PROBLEM - should be 10001
10000
110  

现在的问题是 wordValue 不增加。在硬编码字符串中,wordValue 正确递增。

这是我的总体方法:

try {
    ReadFile files = new ReadFile(file.getPath());
    String[] anyLines = files.OpenFile();

    int i;

    //  test if the program actually read the file
    for (i=0; i<anyLines.length; i++) {
        String input = anyLines[i];
        String[] lines = input.split("\n");

        int wordValue = 16; // to keep track words that are already used
        Map<String, Integer> wordValueMap = new HashMap<String, Integer>();

        for (String line : lines) {
            // if line doesn't begin with "@", then ignore it
            if ( ! line.startsWith("@")) {
                continue;
            }

            // remove &
            line = line.substring(1);

            Integer binaryValue = null;

            if (line.matches("\\d+")) {
                binaryValue = Integer.parseInt(line);
            }
            else if (line.matches("\\w+")) {
                binaryValue = wordValueMap.get(line);

                // if the map doesn't contain the word value,
                // then assign and store it
                if (binaryValue == null) {
                    binaryValue = wordValue;
                    wordValueMap.put(line, binaryValue);
                    wordValue++;
                }
            }

            // I'm using Commons Lang's 
            // StringUtils.leftPad(..) to create the zero padded string
            System.out.println(Integer.toBinaryString(binaryValue));
        }
    }
}

您能给我指出正确的方向吗?

In a nutshell, here's what I'm trying to do:

I want to read a file and detect if the character after the symbol is a number or a word. If it is a number, I want to delete the symbol in front of it, translate the number into binary and replace it in the file. If it is a word, I want to set the characters to number 16 at first, but then, if another word is used, I want to add the 1 to the original number and continue looping till it reaches the end of the input.

When I hard code the string that I want to input and read:

String input = "@5\n@word1\n@word2\n@word1\n@6";
String[] lines = input.split("\n"); // divide up the array

Or:

@5
@word1
@word2
@word1
@6

Then it outputs what I want it to output:

101
10000
10001
10000
110

But when I input anyLines[i] (an array that contains the file info that can change if another file is chosen):

String input = anyLines[i];
String[] lines = input.split("\n");

For the same data, suddenly it gives an incorrect output:

101
10000
10000 <-- PROBLEM - should be 10001
10000
110  

Now the problem with this is that the wordValue doesn't increment. In the hard-coded string, wordValue incremented correctly.

Here's my overall method:

try {
    ReadFile files = new ReadFile(file.getPath());
    String[] anyLines = files.OpenFile();

    int i;

    //  test if the program actually read the file
    for (i=0; i<anyLines.length; i++) {
        String input = anyLines[i];
        String[] lines = input.split("\n");

        int wordValue = 16; // to keep track words that are already used
        Map<String, Integer> wordValueMap = new HashMap<String, Integer>();

        for (String line : lines) {
            // if line doesn't begin with "@", then ignore it
            if ( ! line.startsWith("@")) {
                continue;
            }

            // remove &
            line = line.substring(1);

            Integer binaryValue = null;

            if (line.matches("\\d+")) {
                binaryValue = Integer.parseInt(line);
            }
            else if (line.matches("\\w+")) {
                binaryValue = wordValueMap.get(line);

                // if the map doesn't contain the word value,
                // then assign and store it
                if (binaryValue == null) {
                    binaryValue = wordValue;
                    wordValueMap.put(line, binaryValue);
                    wordValue++;
                }
            }

            // I'm using Commons Lang's 
            // StringUtils.leftPad(..) to create the zero padded string
            System.out.println(Integer.toBinaryString(binaryValue));
        }
    }
}

Can you point me in the right direction please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情 2024-11-17 07:05:58

乍一看,代码看起来还不错。您最好的选择是在处理这些行时打印出这些行,看看它们是否具有……奇怪的……格式:

for (String line : lines)
    System.out.println ("[" + line + "]");

事实上,我会全力以赴,并在每行之后添加一个打印语句更改某些内容(打印序列号和更改的内容),以确保不会出现意外效果:

类似于:

else if (line.matches("\\w+")) {
    binaryValue = wordValueMap.get(line);
    System.out.println ("A: binval set to " + binaryValue);

    // if the map doesn't contain the word value,
    // then assign and store it
    if (binaryValue == null) {
        binaryValue = wordValue;
        System.out.println ("B: binval set to " + binaryValue);
        wordValueMap.put(line, binaryValue);
        System.out.println ("C: put " + binaryValue +
            ", now " + wordValueMap.get(line));
        wordValue++;
        System.out.println ("C: wordval set to " + wordValue);
    }
}

这种 printf 调试方法通常非常有用,尽管您也可以选择使用调试工具:-)

The code looks okay at first glance. Your best bet is to probably print out the lines as you process them to see if they're of a ... strange ... format:

for (String line : lines)
    System.out.println ("[" + line + "]");

In fact, I'd go all out, and put a print statement after every line that changes something (which prints a sequence number and the changed thing), so as to be sure there are no unintended effects:

Something like:

else if (line.matches("\\w+")) {
    binaryValue = wordValueMap.get(line);
    System.out.println ("A: binval set to " + binaryValue);

    // if the map doesn't contain the word value,
    // then assign and store it
    if (binaryValue == null) {
        binaryValue = wordValue;
        System.out.println ("B: binval set to " + binaryValue);
        wordValueMap.put(line, binaryValue);
        System.out.println ("C: put " + binaryValue +
            ", now " + wordValueMap.get(line));
        wordValue++;
        System.out.println ("C: wordval set to " + wordValue);
    }
}

This printf method of debugging can often be invaluable though you could also chose to use a debugging tool :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文