I just stumbled replacing an escaped Unicode of CARRIAGE RETURN / NEW
LINE and got a compiler-error "illegal line end in character literal"
for
switch (original)
{
case '\u000D' : return '\u0020';
case '\u000A' : return '\u0020';
}
To be honest: I don't have the slightest idea what the compiler is
complaining about. The escape-sequence \u seems to be correct
according to most of the sources I came across and the two-digited
hexadecimal encoding cannot be wrong - so what's up here?
Could anybody give me a hint?
Thanks in advance,
Christian
Use \r instead of \u000D and \n for \u000A.
For background and reasons, google for "u000a" (or "u000d")
> case '\u000D' : return '\u0020';
> case '\u000A' : return '\u0020';
> Could anybody give me a hint?
What Andreas said. The compiler actual pre-processes \uxxxx and feeds
the resulting character to the rest of the compiler. So your compiler
is actually seeing a carriage return and a new line there, just as if
you had pressed Enter. Which is illegal.
use less wide indentation, please.
> case '\u000A' : return '\u0020';
>
> }
>
> To be honest: I don't have the slightest idea what the compiler is
> complaining about. The escape-sequence \u seems to be correct
> according to most of the sources I came across and the two-digited
> hexadecimal encoding cannot be wrong - so what's up here?
You ended the line after the keyword 'case' without giving it a
constant. Bear in mind that Unicode escape sequences are substituted
prior to parsing the source, so for example, your second 'case' is
exactly equivalent to:
case '
' : return ' ';
Do you see how that is not a legal Java syntax?
--
Lew
The \u is not a character escape sequence, but a preprocessing
token--these are processed even before lexical scanning.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth