user1621988
user1621988

Reputation: 4335

Special Character in Java

How can i use special characters in java? They belong under the Cp1252 Character encoding. I try to use them in a message, but i cant use them.

Characters like: " ︻ー┳═デ "

Upvotes: 2

Views: 2461

Answers (3)

Isaac
Isaac

Reputation: 16736

When you use string literals with special characters inside Java code, you must also inform the Java compiler which encoding the Java file itself is encoded with.

Say you edited your Java file (containing the Japanese literal text) and saved it as a UTF-8 file. You must, then, tell the compiler to treat the source file as a UTF-8 document:

javac -encoding UTF-8 file1 file2 ...

Otherwise, the Java compiler will assume that the Java file is encoded using the operating system's default encoding, which isn't necessarily what you want it to do.

Upvotes: 1

nullpotent
nullpotent

Reputation: 9260

You might wrap PrintWriter around System.out, to set charset explicitly.

final String charset = "UTF-8";
PrintWriter out = null;
try {
    out = new PrintWriter(new OutputStreamWriter(System.out, charset), true);
} catch(UnsupportedEncodingException e) {
    e.printStackTrace();
}

As for the ? as the character display - It really depends on where you're outputting standard out stream. In standard Ubuntu terminal - it would probably look good (I could test it later). On Windows' cmd however, you should manually set the codepage Chcp and provide a font that comes with those characters.

Same goes for Eclipse, its console charset is UTF-8 I believe, but if you have no fonts with Japanese characters, it won't help you much.

Upvotes: 0

David Soroko
David Soroko

Reputation: 9086

Use the associated UTF values, for example looking up 'デ' on http://www.fileformat.info/info/unicode/char/search.htm gives the value of \u30C7 so you can:

System.out.println( "\u30C7" );  // as string

System.out.println( '\u30C7' ); // as char

Upvotes: 3

Related Questions