1

I'm making next record in order to compare entered chars with this array:

 String[] GSM_7_BIT_EXT = {"\u000c", "\u005e", "\u007b", "\u007d", "\u005c", "\u005b", "\u007e", "\u005d", "\u007c", "\u20ac"};


After compile (Android Studio) getting lot of mistakes, like:
- '}' expected
- illegal character: \35
- expected
and many others.

What am I doing wrong?

DevT
  • 4,843
  • 16
  • 59
  • 92
Goltsev Eugene
  • 3,325
  • 6
  • 25
  • 48

1 Answers1

4

The culprit is \u005c: This is the backslash character, therefore "\u005c" is equal to "\" which is not a valid string literal. (Test this by removing "\u005c" from the array definition).

You could write "\\" instead.

wero
  • 32,544
  • 3
  • 59
  • 84
  • Thanks a lot! This works. Please can you share list of such invalid string literals? Cannot find it in google. – Goltsev Eugene Dec 26 '15 at 16:28
  • @GoltsevEugene: the backslashs starts a escape sequence, see http://stackoverflow.com/a/1367339/3215527, therefore to write a string literal with a backslash character you need to write it as \\. – wero Dec 26 '15 at 16:31
  • What's actually happening is that, unlike all other escape sequences, `\u` is not specific to Strings or chars. It can appear outside of String and char literals, and is parsed before anything else. You could, for example, write `\u0053\u0074\u0072\u0069\u006e\u0067[]` instead of `String[]` and it would compile just fine. That's why `"\u005c"` is equivalent to writing `"\"` in code, which is not legal syntax. – VGR Dec 26 '15 at 16:41
  • Thanks, I understood that. But what other symbols are illegal inside String? – Goltsev Eugene Dec 26 '15 at 17:08
  • @GoltsevEugene \ and " cannnot simply be placed in a string but need to be escaped. – wero Dec 26 '15 at 17:19