【问题标题】:Checking if a character is an integer or letter检查字符是整数还是字母
【发布时间】:2011-09-01 07:44:52
【问题描述】:

我正在使用 Java 修改文件。这是我想要完成的:

  • 如果在读取时检测到 & 符号和整数,我想删除 & 符号并将整数转换为二进制。
  • 如果在读取时检测到 & 符号以及(随机)单词,我想删除 & 符号并将单词替换为整数 16,并且如果正在使用不同的字符串& 符号,我想将数字 1 设置为大于整数 16。

这是我的意思的一个例子。如果输入的文件包含这些字符串:

&myword
&4
&anotherword
&9
&yetanotherword
&10
&myword

输出应该是:

&0000000000010000 (which is 16 in decimal)
&0000000000000100 (or the number '4' in decimal)
&0000000000010001 (which is 17 in decimal, since 16 is already used, so 16+1=17)
&0000000000000101 (or the number '9' in decimal)
&0000000000010001 (which is 18 in decimal, or 17+1=18)
&0000000000000110 (or the number '10' in decimal)
&0000000000010000 (which is 16 because value of myword = 16)

这是我目前尝试过的,但还没有成功:

for (i=0; i<anyLines.length; i++) {
            char[] charray = anyLines[i].toCharArray();
            for (int j=0; j<charray.length; j++)
                      if (Character.isDigit(charray[j])) {
                          anyLines[i] = anyLines[i].replace("&","");
                          anyLines[i] = Integer.toBinaryString(Integer.parseInt(anyLines[i]);
                          }
                       else {
                          continue;
                            }
                        if (Character.isLetter(charray[j])) {
                          anyLines[i] = anyLines[i].replace("&","");
                          for (int k=16; j<charray.length; k++) {
                            anyLines[i] = Integer.toBinaryString(Integer.parseInt(k);
                            }

                        }

                     }
                    }

我希望我表达得足够清楚。有关如何完成此任务的任何建议?

【问题讨论】:

  • 您要检测&amp; 还是%
  • 很抱歉 - 现在是正确的

标签: java file binary character integer


【解决方案1】:
Character.isLetter() //tests to see if it is a letter
Character.isDigit() //tests the character to

【讨论】:

    【解决方案2】:

    看起来你可以匹配正则表达式。我不了解 Java,但您应该至少有一个正则表达式引擎供您使用。那么正则表达式将是:

    正则表达式1: &(\d+) 和 正则表达式2:&(\w+)

    正则表达式3:&(\d+|\w+)

    在第一种情况下,如果 regex1 匹配,您知道您遇到了一个数字,并且该数字在第一个捕获组中(例如:match.group(1))。如果 regex2 匹配,你知道你有一个词。然后,您可以在字典中查找该单词并查看其关联编号是什么,或者如果不存在,则将其添加到字典中并将其与下一个空闲编号相关联(16 + 字典大小 + 1)。

    另一方面,regex3 将匹配数字和单词,因此您可以自行查看捕获组中的内容(这只是一种不同的方法)。

    如果两个正则表达式都不匹配,那么您的序列无效,或者您需要一些其他操作。请注意,正则表达式中的 \w 仅匹配单词字符(即:字母、_ 和可能的一些其他字符),因此 &çSomeWord 或 &*SomeWord 根本不匹配,而 &Hello.World 中捕获的组将只是 "你好”。

    正则表达式库通常会为匹配的文本提供一个长度,因此您可以将 i 向前移动这么多以跳过已经匹配的文本。

    【讨论】:

      【解决方案3】:
      • 您必须以某种方式标记您的输入。似乎您将其分成几行,然后单独分析每一行。如果这是你想要的,好吧。如果没有,您可以简单地搜索&amp; (indexOf('%')),然后以某种方式确定下一个标记是什么(数字或“单词”,但是您想定义单词)。
      • 您想如何处理与您的模式不匹配的输入?任务的描述和示例都没有真正涵盖这一点。
      • 您需要有一个已读取字符串的字典。使用Map&lt;String, Integer&gt;

      【讨论】:

        【解决方案4】:

        我会将此作为评论发布,但还没有能力。您遇到的问题是什么?错误?结果不正确? 16 没有正确递增?此外,这些示例使用“%”,但在您的描述中您说它应该以“&”开头。

        Edit2:认为它是逐行的,但重新阅读表明您可能试图找到说“我去了 &store”并希望它说“我去了 &000010000”。因此,您希望按空格分割,然后遍历字符串并将其传递到您的“替换”方法中,该方法类似于下面。

        Edit1:如果我理解你想要做什么,这样的代码应该可以工作。

        Map<String, Integer> usedWords = new HashMap<String, Integer>();
            List<String> output = new ArrayList<String>();
            int wordIncrementer = 16;
            String[] arr = test.split("\n");
            for(String s : arr)
            {
                if(s.startsWith("&"))
                {
                    String line = s.substring(1).trim(); //Removes &
                    try
                    {
                        Integer lineInt = Integer.parseInt(line);
                        output.add("&" + Integer.toBinaryString(lineInt));
                    }
                    catch(Exception e)
                    {
                        System.out.println("Line was not an integer.  Parsing as a String.");
                        String outputString = "&";
                        if(usedWords.containsKey(line))
                        {
                            outputString += Integer.toBinaryString(usedWords.get(line));
                        }
                        else
                        {
                            outputString += Integer.toBinaryString(wordIncrementer);
                            usedWords.put(line, wordIncrementer++); 
                        }
                        output.add(outputString);
                    }
                }
                else
                {
                    continue; //Nothing indicating that we should parse the line.
                }
            }
        

        【讨论】:

        • 它没有做我想做的事——它找不到“j”变量
        • test.split 只是您将传递给方法的字符串。在我的示例中,“文件”或您正在解析的任何内容只是一个由换行符分隔的字符串。但是,正如我的第二次编辑所示,您可能希望用空格分割您的行。但这应该给你一个想法。
        【解决方案5】:

        这个怎么样?

        String input = "&myword\n&4\n&anotherword\n&9\n&yetanotherword\n&10\n&myword";
        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
            String out = "&" + StringUtils.leftPad(Integer.toBinaryString(binaryValue), 16, "0");
            System.out.println(out);
        

        这是打印输出:-

        &0000000000010000
        &0000000000000100
        &0000000000010001
        &0000000000001001
        &0000000000010010
        &0000000000001010
        &0000000000010000
        

        仅供参考,10 的二进制值是“1010”,而不是原始帖子中所述的“110”。

        【讨论】:

          猜你喜欢
          • 2012-12-23
          • 2013-10-09
          • 2013-08-05
          • 1970-01-01
          • 2014-02-12
          • 2020-03-20
          • 2018-03-20
          • 2016-11-20
          • 1970-01-01
          相关资源
          最近更新 更多