Reputation: 122
We were asked to make a program that will read a text file and show a summary of Unicode characters. While doing this I encountered a problem with some Unicode characters that appear to be printed as a question mark in my console. However when I output the same Unicode text using Swing, its not a question mark anymore
System.out.println("\u0126"); // appears to be ? in my console.
JOptionPane.showMessageDialog(null,"\u0126"); // seems to display the character successfuly
I could just leave the problem behind since I'm gonna make use of GUI, but I want an explanation, something that beginners like me could understand.
Why do some Unicode characters appear to be a question mark printed in the console but does not in Swing, printing correctly? (Eclipse, NetBeans, JCreator, JGrasp do the same, I thought its a problem with my IDE).
Is it a problem in Encoding or Font? And what should I do in order to successfully display Unicode text in the console without any trouble of question marks in the future?
Upvotes: 3
Views: 11446
Reputation: 11
How to insert emojis and fancy Unicode Characters in Java?
It took me a while to find a solution by my own, hope it helps :)
// To run emojis in Java make sure you have installed Windows Terminal
// and in order to insert them, remember Java char only support 16-bit
// characters meanwhile a great part of Unicode is 32-bit sooooo
// every single Unicode character must be break into two 16-bit
// character, these are called surrogates.
// How to create surrogates and use emojis in Java! :D
//1. Copy Uni code you want to use (Ommit U+, that's only a label)
// https://unicode-table.com/en/1F680/
//2. Paste in here and copy both surrogates
// http://www.russellcottrell.com/greek/utilities/SurrogatePairCalculator.htm
//3. Code it as Strings and separate it by \u symbol in Java.
//4. Compile it and run it in Windows Terminal.
// #HappyCoding!!
// One small thing: For some reason you need to put two spaces after
// every two surrogates, otherwise it'll print two question marks next
// to it.
// Other small thing: Try to use emojis at the end of a line because for
// some reason if you insert String after the Unicode it'll do strange
// things.
String rocket = "Hello Entrepeneurs \uD83D\uDE80 ";
System.out.print(rocket);
Upvotes: 1
Reputation: 17573
The characters generated by UNICODE will depend on the character gylphs that are part of the font that you are using. Most fonts have only a subset of the complete UNICODE standard. For instance if you are wanting to display Simplified Chinese the font you are using must have the glyphs for Simplified Chinese.
The UNICODE Consortium has some information about this.
Upvotes: 4
Reputation: 2238
u0126 -> is a character outside of the ANSCII (or whatever it is called) that most terminals output. The character is actually Ħ, but this will only show if your console can show that character.
You can see if installing utf-8 might allow you to show characters like that.
Upvotes: 0