2

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: " ︻ー┳═デ "

user1621988
  • 4,215
  • 8
  • 27
  • 31
  • Are you trying to write the characters in the source code or e.g. printing them to output? Can you please post some code showing how you try to use the characters. – Nicholas Sep 16 '12 at 09:07
  • 2
    Japanese katakana DE is **not** in CP1252 – ninjalj Sep 16 '12 at 09:08
  • @ ninjalj when i try to save it, it gives the CP1252 error @ Nicholas i use an external API. player.sendMessage(ChatColor.BLUE + victim.getName() + ChatColor.WHITE + "︻デ┳═ー" + ChatColor.RED + attacker.getName()); – user1621988 Sep 16 '12 at 09:11
  • Neither are `U+FE3B PRESENTATION FORM FOR VERTICAL LEFT BLACK LENTICULAR BRACKET`, `U+30FC KATAKANA-HIRAGANA PROLONGED SOUND MARK `, `U+2533 BOX DRAWINGS HEAVY DOWN AND HORIZONTAL`, `U+2550 BOX DRAWINGS DOUBLE HORIZONTAL` – ninjalj Sep 16 '12 at 09:12
  • You SHOULD specify explicitly your output (and input) encoding. – ninjalj Sep 16 '12 at 09:13

3 Answers3

3

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
David Soroko
  • 8,521
  • 2
  • 39
  • 51
1

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.

Isaac
  • 16,458
  • 5
  • 57
  • 81
0

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.

nullpotent
  • 9,162
  • 1
  • 31
  • 42