Is there a way to read the new unicode characters or glyphs I guess, like the Ever Green Green (U+1F332), the mountain (U+26F0), and the Man in a suit (U+1F574) in java? I really want to implement this into a game I'm writing but I don't know how. Apache's extension library is not acceptable, sorry.
-
What have you tried? How has it failed? As far as I know, Java itself has no special restriction to characters defined in any particular version of Unicode. – John Bollinger Jan 28 '16 at 20:17
-
2Do note, however, that *reading* characters is worlds different from printing / displaying graphic representations of them. The latter requires use of a font that has glyphs for the characters in question; that might be hard to come by for characters that have only recently been added. – John Bollinger Jan 28 '16 at 20:19
-
Usually [the Java tutorials](http://docs.oracle.com/javase/tutorial/i18n/text/unicode.html) are a great place to start when you have "How is this common action done in Java?" questions. – Linus Jan 28 '16 at 20:28
2 Answers
As I implied in comments, your problem is unlikely to be with reading the characters. Java should be able to read them (and write them) just fine. For example:
import java.io.*;
public class NewChars {
public static void main(String[] args) throws IOException {
// UTF-16 (surrogate pairs) for the two characters mentioned:
String theChars = "\ud87c\udf32\ud87e\udd74";
// Write the characters to a (UTF-8 encoded) byte stream:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Writer out = new OutputStreamWriter(baos, "UTF-8");
out.write(theChars);
out.close();
// Read the characters back
BufferedReader in = new BufferedReader(new InputStreamReader(
new ByteArrayInputStream(baos.toByteArray()), "UTF-8"));
String readBack = in.readLine();
// Check the result (prints "Readback is good"):
System.out.println(theChars.equals(readBack) ? "Readback is good"
: "Readback is bad");
}
}
The problem is very likely that your system does not have a font that contains glyphs for those characters. The glyph you describe seeing -- a square or diamond with a question mark inside -- is one of the commonly-used substitution glyphs emitted in a place of a character for which the chosen font has no other glyph.
This is not a Java problem. You need updated fonts, probably from your OS vendor, and they may or may not be available. Moreover, it would be wise to avoid depending on characters that others might not have supporting fonts for, either, unless you only intend for your program to work as intended on the computer on which you develop it.

- 160,171
- 8
- 81
- 157
-
Thanks for the explanation, this makes more sense and I can't require my professor to upgrade his system. LOL – White Lotus Jan 28 '16 at 20:53
Try this
System.out.println(new String(Character.toChars(0x1f332)));
System.out.println(new String(Character.toChars(0x26f0)));
System.out.println(new String(Character.toChars(0x1f574)));
First and third characters are surrogate pairs.
-
-
That maybe [this question](http://stackoverflow.com/q/20386335/2654103) is relevant. (If you are using a console in windows) – Linus Jan 28 '16 at 20:41
-
This works fine for me but my professor is a mac user so it doesn't work for hiim. – White Lotus Jan 28 '16 at 20:46