If you want to have a string literal with a special character, you can try using a Unicode escape:
String [] Norge = {"l\u00F8k", "h\u00E5r", "v\u00E5r", "s\u00E6r", "s\u00F8t"};
While it is not wrong to include special characters in source code (at least in java), it can in some cases cause problems with poorly configured editors, compilers, or terminals; Personally I steer clear of using special characters at all if I can.
Incidentally, you can also use Unicode escapes elsewhere in java source code, including javadoc comments, and class, method, and variable names.
If you are compiling from the command line, you can configure the compiler to accept UTF-8 by using the -encoding
option with UTF-8
as its parameter. Like so:
javac -encoding UTF-8 ...
You may also find this question useful: Special Character in Java
You might consider externalizing the strings, as an alternate way to solve the problem. Eclipse provides a way to automatically do this, but it basically just takes all the literal strings, puts them in a separate file, and reads from that file to get the appropriate string. This also allows you to create a translation of the program, by making a different file with translations of all the strings, or to reconfigure application messages without having to recompile.
EDIT: I just tried compiling and running it myself (in eclipse), and I did not have the problem with it you mention. It is therefore likely an issue with your particular setup.
When I reconfigured it to compile the code as US-ASCII, it output l?k
both times.
When I reconfigured it to compile the code as UTF-8, the output was løk
and løk
.
When I compiled it as UTF-16, the output was þÿ l ø k
and løk
, however I could not copy the blank spaces in þÿ l ø k
from the terminal: it would let me copy the first two, but leave off the rest. This is probably related to the issue you were having - they could be some control characters that are messing it up in your case.