【问题标题】:Reading characters and checking if they're a number读取字符并检查它们是否是数字
【发布时间】:2011-05-26 02:32:20
【问题描述】:

我正在尝试一种方法来检查符号 & 后面的字符是数字还是字母;如果是数字,则将其转换为二进制;如果是字母,则设置为 16,如果使用不同的单词,则加 1。问题是,由于某种原因,这对我不起作用。有什么建议么?

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

       int i;

       for (i=0; i<anyLines.length; i++) {
           String input = anyLines[i];
           String[] lines = input.split("\n");

           int wordValue = 16;

           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.toBinaryString(131072 +
                              Integer.parseInt(anyLines[i])).substring(2,18);
               }
               else if (line.matches("\\w+")) {
                   binaryValue = wordValueMap.get(line);

                   if (binaryValue == null) {
                       binaryValue = wordValue;
                       wordValueMap.put(line, binaryValue);
                       wordValue++;
                   }
               }
           }
       }

输入:

&4
...
&hello
...
&ok

输出:

(5 translated into binary) : 0000000000000100
...
(16 translated into binary)
...
(17 translated into binary, or 16+1)

这是你的方法的输出:

101
1001100
1001100
1001100
1001100
1001100
1001100
1001100
&5
1110110000010000 
&hello
1110001100001000 
&goodbye
1110101010001000 
(NEXT)
&goodbye
1111000010001000 
&hello
1110001110001000 
&BILL
1110001100000110 
&NEXT
1110101010000111 
(BILL)
&BILL
1110101010000111 

这是我正在阅读和循环的原文(anyLines[i] 没有任何修改):

&5
var1 
&hello
var2 
&goodbye
var2 
(NEXT)
&goodbye
var3 
&hello
var4 
&BILL
var5 
&NEXT
var6 
(BILL)
&BILL
var5 

var 只是一个有值的变量。我已经处理好了。


这是我的尝试:

String input = "This is a test line\n"
           + "&hello\n"
           + "&4\n"
           + "&32";

String[] lines = input.split("\n");
int wordValue = 26;

Map<String, Integer> wordValueMap = new HashMap<String, Integer>();

for (String currentLine : lines)
{
if (!currentLine.startsWith("&"))
{
    continue;
}

currentLine = currentLine.substring(1);
Integer value;

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

    if(value == null)
    {
        int binaryValue = wordValue++;
        wordValueMap.replace(currentLine, binaryValue);
        /*
         * This is just there to ensure that the print statement below doesn't have a 
         * null value.
         */
        value = binaryValue;
    }
}
else
{
    System.out.println("Invalid input");
    break;
}

System.out.println(Integer.toBinaryString(value));
}

【问题讨论】:

  • 你能描述一下它“不起作用”的方式吗?
  • 它只是保留了找到的字符串,带有“&”符号,即使我明确表示我不想要它
  • 您能否简化代码并缩小问题所在?回答问题很有趣,但也有很多。添加一些示例输入和输出也会有所帮助。
  • 还有toBinaryString方法中131072的作用是什么?
  • @Varun,它是一种对二进制数进行零填充的方法。 ;)

标签: java file integer character letter


【解决方案1】:

您的代码存在许多问题。 首先,您必须解析在 toBinaryString() 调用之后获得的值。

binaryValue = Integer.parseInt(Integer.toBinaryString(131072 +
                          Integer.parseInt(anyLines[i])).substring(2,18));

在该代码中使用 anyLines[i] 作为 parseInt() 函数的参数也是不正确的。根据我对您的代码的了解,anyLines[i] 是一个包含 newLines 和其他内容的字符串,显然无法将其解析为整数。

下面的代码似乎有效。我已经更改了 HashMap 的存储,以便它存储常规整数,而不是尝试将二进制表示形式存储为整数值。

String input = "This is a test line\n"
               + "&hello\n"
               + "&4\n"
               + "&32";

String[] lines = input.split("\n");
int wordValue = 26;

Map<String, Integer> wordValueMap = new HashMap<String, Integer>();

for (String currentLine : lines)
{
    if (!currentLine.startsWith("&"))
    {
        continue;
    }

    currentLine = currentLine.substring(1);
    Integer value;

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

        if(value == null)
        {
            int binaryValue = wordValue++;
            wordValueMap.put(currentLine, binaryValue);
            /*
             * This is just there to ensure that the print statement below doesn't have a 
             * null value.
             */
            value = binaryValue;
        }
    }
    else
    {
        System.out.println("Invalid input");
        break;
    }

    System.out.println(Integer.toBinaryString(value));
}

作为旁注,我希望您知道,如果出现数字后跟单词(如句子)的情况,这将失败。但是,如果您的输入永远不会有任何这些,那不是问题。

【讨论】:

  • +1 我认为这是主要问题。在完成从anyLines[i] 创建line 的所有工作之后,这段代码简单地回到原来的行。
  • 我花了很多时间试图弄清楚这段代码做了什么。
  • 那么你建议我应该怎么做?
  • 我建议您将值存储为整数,而不是尝试将它们存储为二进制字符串。然后,当您出于某种原因需要将它们转换为二进制字符串时,请使用 toBinaryString() 方法。
  • 我唯一的问题是它读取文件并且不增加单词值 - 如果一行读取 @hello 而另一行读取 @goodbye ,两个单词的值相同 - 16,但是@hello 的值应该是 16,@goodbye - 17 因为值 16 已经专为 @hello 保留。
猜你喜欢
  • 2015-05-25
  • 2020-04-28
  • 1970-01-01
  • 2018-07-30
  • 2021-02-28
  • 2017-08-12
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多