I'm trying to write unicode characters (♠) using System.out, and a question mark gets printed instead.
How can I have proper unicode characters displayed instead of question marks?
I'm using IntelliJ IDEA on Windows, and trying to print from within the IDE.
How to show tabs and white space characters in IDEA? To enable this feature in IntelliJ, you must open the Settings dialog ( Ctrl+Alt+S ) and navigate to the Editor | General | Appearance tab. In this tab you need to enable the Show whitespace option.
In computing, the question mark character is represented by ASCII code 63 (0x3F hexadecimal), and is located at Unicode code-point U+003F ? QUESTION MARK ( ?).
Go to Help > Edit Custom VM options... then add the following option:
-Dconsole.encoding=UTF-8
-Dfile.encoding=UTF-8
I'm not sure if both are necessary but it worked for me. You need to restart IntelliJ for changes to be applied.
I had already tried changing every encoding setting in Intellij, setting those options in Gradle and changing the system encoding, this is the only one that worked.
TL;DR answer:
Go to Settings
-> Editor
-> File Encodings
-> Project Encoding
and set it to "UTF-8".
Expanded answer:
The reason why it does not work can be found by placing a breakpoint on a System.out.print()
call. When the breakpoint hits, you can add System.out
to Watches
, and you can see that System.out.textOut.out.se.cs
is set to windows-1252
or something similarly unsuitable.
The setting which magically worked for me (I do not know why) is in Settings
-> Editor
-> File Encodings
-> Project Encoding
. You need to set that to "UTF-8".
Then, unicode characters display properly on the console, and one more quick look with the debugger shows that the value of System.out.textOut.out.se.cs
has magically turned into UTF-8
.
I am saying "magically" because I do not see how and why an editor setting should affect the character set that System.out
gets instantiated with when launching/debugging an application. If someone knows what is the logic behind this, please do tell!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With