2

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?

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot! This works. Please can you share list of such invalid string literals? Cannot find it in google.
@GoltsevEugene: the backslashs starts a escape sequence, see stackoverflow.com/a/1367339/3215527, therefore to write a string literal with a backslash character you need to write it as \\.
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.
Thanks, I understood that. But what other symbols are illegal inside String?
@GoltsevEugene \ and " cannnot simply be placed in a string but need to be escaped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.