views:

674

answers:

4

On Mac OS X 10.5 with Java 1.5, I have this code snippet.

public static void main(String[] args) {
    String name = "Алексей";
    System.out.println("name = " + name);
}

The output is:

name = ???????

I expected to see:

name = Алексей

How can I make this happen?

+4  A: 

There are two potential problems here. First, you have Cyrillic characters in your source code. This means that you have to ensure the encoding that you used to store the file is the same as that used by the compiler. Check out the javac manpage to see what its default encoding is, and/or how to specify a specific encoding. Or, use Unicode escapes within your string.

Second possibility is that your terminal font doesn't display the Cyrillic characters. I don't think this is what's happening, however, since (1) you're able to see the characters in the source code, and (2) I believe missing glyphs render as blocks, incorrect encodings render as question marks.

To test, simply replace your string with the correct Unicode escapes, and verify that the characters render on your screen.

kdgregory
+2  A: 

If you use the Console class instead of System.out, and you're really really lucky, you MIGHT be able to get the console to print cyrillic characters.

But in general, consoles don't have good character set support.

It would be much much more reliable to output your string in some other way, e.g. printing it to a Swing component.

Neil Coffey
+1  A: 

If your string is is dynamically got from somewhere and you need to make sure it's encoded as you need, use some of String's class constructors that take Charset type as a parameter to convert that string.

HappyCoder
+1  A: 

Thanks to all the helpful ideas.

It turns out the key to my problem was this sentence:

On Mac OS X 10.5 with Java 1.5, I have this code snippet

The IDE I'm using, IntelliJ IDEA was explicitly setting the file encoding to MacRoman. By changing the encoding for the project to UTF-8, the problem was solved without any code changes.

Steve McLeod