开发者

Checking if a character is an integer or letter

I am modifying a file using Java. Here's what I want to accomplish:

  • if an & symbol, along with an integer, is detected while being read, I want to drop the & symbol and translate the integer to binary.
  • if an & symbol, along with a (random) word, is detected while being read, I want to drop the & symbol and replace the word with the integer 16, and if a different string of characters is being used along with the & symbol, I want to set the number 1 higher than integer 16.

Here's an example of what I mean. If a file is inputted containing these strings:

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

The output should be:

&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)

Here's what I tried so far, but haven't succeeded yet:

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++) {
  开发者_如何学Go                          anyLines[i] = Integer.toBinaryString(Integer.parseInt(k);
                            }

                        }

                     }
                    }

I hope that I am articulate enough. Any suggestions on how to accomplish this task?


Character.isLetter() //tests to see if it is a letter
Character.isDigit() //tests the character to


It looks like something you could match against a regex. I don't know Java but you should have at least one regex engine at your disposal. Then the regex would be:

regex1: &(\d+) and regex2: &(\w+)

or

regex3: &(\d+|\w+)

in the first case, if regex1 matches, you know you ran into a number, and that number is into the first capturing group (eg: match.group(1)). If regex2 matches, you know you have a word. You can then lookup that word into a dictionary and see what its associated number is, or if not present, add it to the dictionary and associate it with the next free number (16 + dictionary size + 1).

regex3 on the other hand will match both numbers and words, so it's up to you to see what's in the capturing group (it's just a different approach).

If neither of the regex match, then you have an invalid sequence, or you need some other action. Note that \w in a regex only matches word characters (ie: letters, _ and possibly a few other characters), so &çSomeWord or &*SomeWord won't match at all, while the captured group in &Hello.World would be just "Hello".

Regex libs usually provide a length for the matched text, so you can move i forward by that much in order to skip already matched text.


  • You have to somehow tokenize your input. It seems you are splitting it in lines and then analyzing each line individually. If this is what you want, okay. If not, you could simply search for & (indexOf('%')) and then somehow determine what the next token is (either a number or a "word", however you want to define word).
  • What do you want to do with input which does not match your pattern? Neither the description of the task nor the example really covers this.
  • You need to have a dictionary of already read strings. Use a Map<String, Integer>.


I would post this as a comment, but don't have the ability yet. What is the issue you are running into? Error? Incorrect Results? 16's not being correctly incremented? Also, the examples use a '%' but in your description you say it should start with a '&'.

Edit2: Was thinking it was line by line, but re-reading indicates you could be trying to find say "I went to the &store" and want it to say "I went to the &000010000". So you would want to split by whitespace and then iterate through and pass the strings into your 'replace' method, which is similar to below.

Edit1: If I understand what you are trying to do, code like this should work.

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.
        }
    }


How about this?

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);

Here's the printout:-

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

Just FYI, the binary value for 10 is "1010", not "110" as stated in your original post.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜