Invalid character constant in java

return (int) (feetPart) + '\' ' + inchesPart + '\''+'\''; 

Why the specified character constant is invalid, this works fine in JavaScript. I want to display the height in feet and inches and used this client side, but when I use it on the server side, it shows Invalid character constant .

+4
source share
1 answer

Why the specified invalid character constant

Because of this part:

 '\' ' 

This is an attempt to specify a character literal, which is actually two characters (apostrophe and space). A literal character must be exactly one character.

If you want to specify an "apostrophe space", you should use a string literal instead - at this point the apostrophe should not be escaped:

 "' " 

All your statement will be better:

 return (int) (feetPart) + "' " + inchesPart + "''"; 

Or use " instead '' for inches:

 return (int) feetPart + "' " + inchesPart + "\""; 

Please note that itโ€™s not even clear to me that the source code would do what you would like it to compile, since I suspect that it would perform integer arithmetic on feetPart and the symbol ...

Your code would be fine in Javascript because both single quotes and double quotes are used for string literals.

+18
source

All Articles