• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Printing chinese character to system console in Java

key78

n00b
Joined
Mar 7, 2005
Messages
29
Hi,

When I try to print a chinese character using
System.out.println("\u4e00");

it outputs a ? instead of the chinese character... what am I doing wrong? Thanks.
 
Are you sure that your console is capable of displaying Chinese characters?
 
Where are you viewing the output, the Windows command prompt? AFAIK 'cmd' doesn't support unicode, and by default it probably uses code page 437 (DOS ANSI?). Also, using System.out.println, from what I have experienced, is not particularly effective for non-ANSI strings, so if you want to output in Unicode explicitly (and not rely on any given system's default encoding), use a PrintStream object with System.out as your OutputStream, and specify the encoding.

Code:
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

public class TestClass {

	public static void main(String [] args) throws UnsupportedEncodingException {
		String chineseString = "\u4e00\u4e01\u4e02\u4e03\u4304";
		PrintStream ps = new PrintStream(System.out, true, "UTF-8");
		ps.println(chineseString);
	}
}

In CMD, this will display something like this...

Code:
一丁丂七䌄
or will be a bunch of ??? depending on your font.

In Eclipse editor, once I specify the console's encoding to UTF-8, I get this...

Code:
一丁丂七
 
In Eclipse editor, once I specify the console's encoding to UTF-8, I get this...

Code:
一丁丂七

I just changed the console encoding to UTF-8 in eclipse, but it's printing a bunch of squares... anything else I'm missing?

Edit: I think it's working now, it was \u4e04 instead of \u4304. Thanks everyone.
 
Back
Top