用数字替换符号 [英] Replacing symbol with a number

查看:175
本文介绍了用数字替换符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要读取一个文件,检测符号后面的字符是数字还是字。如果它是一个数字,我想删除它前面的符号,将该数字转换为二进制文件并将其替换为文件。如果是一个单词,我想首先将字符设置为16,但是如果使用另一个单词,我想将1添加到原始数字。这是我想要的:

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. Here's what I want:

如果文件名读取(...表示不需要翻译的字符串):

If the file name reads (... represents a string that does not need to be translated):

%10
...
%firststring 
...
%secondstring
...
%firststring
...
%11
...

and so on...

我希望它像这样:

0000000000001010 (10 in binary)
...  
0000000000010000 (16 in binary)
...
0000000000010001 (another word was used, so 16+1 = 17 in binary)
...
0000000000010000 (16 in binary)
...
0000000000001011 (11 in binary)

这是我试过的:
anyLines只是一个字符串数组,它具有文件的内容(如果我是说System.out.println(anyLines [i]),我将文件的内容打印出来)

And here's what I tried: anyLines is just a string array which has the contents of the file (if I were to say System.out.println(anyLines[i]), I would the file's contents printed out).

UPDATED! / p>

UPDATED!

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

        int i;


    int wordValue = 16;

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

for (String line : anyLines) {
// 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));

}

现在,我只需要替换符号(%10 ,%firststring等)与二进制值。

Now, I only have to replace the symbols (%10, %firststring, etc) with the binary value.

执行此代码后,输出结果为:

After executing this code, what I get as the output is:

1010 
10000 
10001
10000
1011
%10
...
%firststring 
...
%secondstring
...
%firststring
...
%11
...

现在我只需要用1010替换%10,%firststring与10000等等该文件将如下所示:

Now I just need to replace the %10 with 1010, the %firststring with 10000 and so on, so that the file would read like this:

0000000000001010 (10 in binary)
...  
0000000000010000 (16 in binary)
...
0000000000010001 (another word was used, so 16+1 = 17 in binary)
...
0000000000010000 (16 in binary)
...
0000000000001011 (11 in binary)

你有什么建议关于如何使这项工作?

Do you have any suggestions on how to make this work?

推荐答案

这个可能不会m>正在做你认为的事情:

This may not be doing what you think it's doing:

 int binaryValue = wordValue++;

由于您正在使用后递增运算符,因此将二进制值分配给旧的worldValue值, 然后增加worldValue。我会在两个单独的行上完成增量,首先执行:

Because you are using the post-increment operator, binary value is being assigned the old worldValue value, and then worldValue is incremented. I'd do this on two separate lines with the increment being done first:

 wordValue++;
 int binaryValue = wordValue; // binaryValue now gets the new value for wordValue

编辑1

如果您还需要我们的帮助,建议您执行以下操作:

EDIT 1
OK, if you still need our help, I suggest you do the following:


  • 向我们显示数据示例文件,所以我们可以看到它实际上是什么样子。

  • 解释anyLines数组和lines数组之间的差异以及它们与数据文件的关系。他们都有字符串,线条显然是用\分割anyLines的结果,但是什么是任何线。你声明该文件是一个文本文件,但是如何从这个文本文件中获取字符串的初始数组?是否有另一个分隔符用于获取此数组?您是否尝试通过打印任何线和线的内容来调试代码?

  • 如果您需要使用wordValue来通过anyLines循环遍历每个循环(再次知道这将是什么帮助),您将需要在循环之前声明并初始化

  • 如果您无法创建和发布SSCCE,至少使您的代码格式化一致和可读,类似下面的代码。

  • 查看如何询问智能问题,以获取有关可以帮助我们提供帮助的信息的更多提示。

  • Show us a sample of the data file so we can see what it actually looks like.
  • Explain the difference between the anyLines array and the lines array and how they relate to the data file. They both have Strings, and lines is obviously the result of splitting anyLines with "\n" but what again is anyLines. You state that the file is a text file, but how do you get the initial array of Strings from this text file? Is there another delimiter that you use to get this array? Have you tried to debug the code by printing out the contents of anyLines and lines?
  • If you need wordValue to persist with each iteration of a loop through anyLines (again, knowing what this is would help), you will need to declare and initialize it before the loop.
  • If you can't create and post an SSCCE, at least make your code formatting consistent and readable, something like the code below.
  • Have a look at the link on how to ask smart questions for more tips on information that you could give us that would help us to help you.

示例代码格式:

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

     String[] anyLines = {};

     int i;

     // test if the program actually read the file
     for (i = 0; i < anyLines.length; i++) {

        String[] lines = anyLines[i].split("\n");
        int wordValue = 76;

        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);
                 // TODO: fix below
                 // !! currentLine.replace(currentLine, binaryValue);

                 value = binaryValue;
              }
           } else {
              System.out.println("Invalid input");
              break;
           }

           System.out.println(Integer.toBinaryString(value));
        }
     }
  } finally {
     // Do we need a catch block? If so, catch what?
     // What's supposed to go in here?
  }

运气!

这篇关于用数字替换符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆