Help with Java Code


Senzon
Member
Registered: 25.05.12 19:13
Timezone: UTC +3
Posts: 17

I stumbled on this site and it seemed like a good place to ask for some programming help.

I have this code I wrote to grab all the parameters from strings that look like this "Some text {0}, more text: {1}". The {0} and {1} will eventually be replaced with arguments. I have a class that's supposed to find all the parameter numbers and verify that they're in the right order, but the method to grab all the parameters gets an error for some reason and I dont understand why.

Here's my code:

protected List<Integer> getParameters(String s) {
  List<Integer> results = new ArrayList<Integer>();
  final String openBracket = "{";
  final String closingBracket = "}";
  int indexOfClosingBracket = -1;
  int result = -1;
  if(!s.isEmpty()){
    String[] splitParts = s.split(openBracket);
    for (String splitPart : splitParts) {
      indexOfClosingBracket = splitPart.indexOf(
        closingBracket);
      if (indexOfClosingBracket != -1) {
        String parameterNumber = splitPart.substring(
          0, indexOfClosingBracket);
        try{
          result = Integer.valueOf(parameterNumber);
          results.add(result);
        }catch(NumberFormatException e){
          //must not have been a number inside, ignore it
        } 
      }
    }
  }
  return results;
}

And here is the error I get:

SEVERE: Non-parsable error in main 
java.util.regex.PatternSyntaxException: Illegal repetition
{
    at java.util.regex.Pattern.error(Unknown Source)
    at java.util.regex.Pattern.closure(Unknown Source)
    at java.util.regex.Pattern.sequence(Unknown Source)
    at java.util.regex.Pattern.expr(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.util.regex.Pattern.<init>(Unknown Source)
    at java.util.regex.Pattern.compile(Unknown Source)
    at java.lang.String.split(Unknown Source)
    at java.lang.String.split(Unknown Source)
    at parsables.parsables.ParameterSequenceParsable.getParameters(ParameterSequenceParsable.java:30)
    at parsables.parsables.ParameterSequenceParsable.isMatched(ParameterSequenceParsable.java:61)
    at parsables.main.AParsable.match(AParsable.java:51)
    at parsables.main.AParsable.doParse(AParsable.java:27)
    at parsables.main.AParsable.parse(AParsable.java:65)
    at parsables.main.ParserMain.execute(ParserMain.java:208)
    at parsables.main.ParserMain.main(ParserMain.java:190)

Please let me know if you know what's causing this error.

-Senzon


COM
Member
Registered: 11.09.11 00:50
Timezone: UTC +1
Posts: 10

As it turns out, the split method utilises regex for its split and since brackets are special characters in regex, shit gets fucked up.
Try replacing

final String openBracket = "{";
final String closingBracket = "}";

with

final String openBracket = "\\{";
final String closingBracket = "\\}";

There is probably a more efficient way to do this; however, I'm not very proficient with java, nor do I even like the language. Hopefully ynori will have some pointers for you.


ynori7
Administrator
Registered: 24.08.11 12:16
Timezone: UTC +2
Posts: 130

COM said:
There is probably a more efficient way to do this; however, I'm not very proficient with java, nor do I even like the language. Hopefully ynori will have some pointers for you.

Yeah, it's probably more efficient to use regular expressions with capturing groups, but I don't use regex often enough to know what string to use off-hand. However here is a tutorial on Java's regex clases.

By the way, I don't believe you need the backslash on the closing bracket.

i537.photobucket.com/albums/ff338/ynori77/archenemysig1.jpg


Senzon
Member
Registered: 25.05.12 19:13
Timezone: UTC +3
Posts: 17

Thanks guys, that fixed it. And I'll look at that tutorial and see if I can figure out the regex stuff.