4

I'm getting two errors while compiling this snippet:

map <String,Integer> CharToNumber = new map<String,Integer>(); 

    CharToNumber.put('A', 0); 
    CharToNumber.put('B', 1);

Errors are: 1) expecting a right parentheses, found 'A'
2) UnexpectedSyntaxError(loc = RealLoc(startIndex = 42, endIndex = 43, line = 6, column = 19), message = mismatched input 'A' expecting RPAREN))

Now: I know it's not a common situation, but: can I declare a map of (having String as keys and Integer as values) ?

1
  • 3
    That code executes anonymously without error. – Phil Rymek Sep 9 '14 at 17:06
2

The code you have posted is valid Apex and reasonable. So the problem is either that some surrounding code is interfering - do you have something else called CharToNumber in any casing? - or that you are hitting a bug in the local compiler that is part of the Force.com IDE that you can ignore.

If you just need to initialize the map you can also use this more compact syntax for that:

map <String,Integer> CharToNumber = new map<String,Integer>{
        'A' => 0,
        'B' => 1
        };

that may work-around the problem.

5
  • 1
    +1 on the additional way to initialize. I always forget to mention something..... lol – Chris Duncombe Sep 9 '14 at 17:10
  • 1
    It works, but I can't explain why my code had problems..I think it's a Force.com IDE problem.. – user9959 Sep 9 '14 at 17:13
  • 2
    @toninotrip Yes errors mentioning "RealLoc" are coming from the new (and somewhat buggy) local Apex parser. – Keith C Sep 9 '14 at 18:03
  • @KeithC so now I solved with your alternative syntax..but, what If I could not solve in that way? just for information..was there any other way to keep my map working, having the bug you spoke about, with normal syntax? – user9959 Sep 10 '14 at 7:43
  • 1
    @toninotrip You could move back to an earlier version of the Force.com IDE or (with risk of other problems) a beta of the next version (github.com/forcedotcom/idecore/wiki/…). Or change to another IDE such as MavensMate. – Keith C Sep 10 '14 at 8:01
3

I don't think there is an error in the code shown. I have executed without error in an Apex anonymous window.

I even added a debug statement to show the map is created and the elements are added correctly.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy