I'm trying to use unicode character with netbeans but, when i run the project it only show me '?'. But if I use Intellij all runs perfectly. I do some research but I didn't find any solutions.
I also tried to add -J-Dfile.encoding=UTF-8
inside netbeans.conf
but nothing change.
That's the code:
public class Unicode {
public static void main (String [ ] args){
String unicodeMessage =
"\u2654 " + // white king
"\u2655 " + // white queen
"\u2656 " + // white rook
"\u2657 " + // white bishop
"\u2658 " + // white knight
"\u2659 " + // white pawn
"\n" +
"\u265A " + // black king
"\u265B " + // black queen
"\u265C " + // black rook
"\u265D " + // black bishop
"\u265E " + // black knight
"\u265F " + // black pawn
"\n" ;
String unicodeGeometry = "\uD83D\uDFE8";
System.out.println(unicodeMessage);
System.out.println(unicodeGeometry);
}
}
I tried to modify netbeans.conf
adding -J-Dfile.encoding=UTF-8
at the end of the netbeans_default_options
block. I also runned the netbeans.conf
file like administrator
Verify that your terminal/console app:
FYI, most defaults in modern Java were changed to UTF-8 across all host platforms. See JEP 400: UTF-8 by Default, affecting Java 18+.
Your code works for me, copy-pasted and run in Java 22. I see all the chess pieces. And your final item is an orange-colored square.
System.out.println(Runtime.version() );
String unicodeMessage =
"\u2654 " + // white king
"\u2655 " + // white queen
"\u2656 " + // white rook
"\u2657 " + // white bishop
"\u2658 " + // white knight
"\u2659 " + // white pawn
"\n" +
"\u265A " + // black king
"\u265B " + // black queen
"\u265C " + // black rook
"\u265D " + // black bishop
"\u265E " + // black knight
"\u265F " + // black pawn
"\n" ;
String unicodeGeometry = "\uD83D\uDFE8";
System.out.println(unicodeMessage);
System.out.println(unicodeGeometry);
You can see the code run at Ideone.com.
12.0.1+12
♔ ♕ ♖ ♗ ♘ ♙
♚ ♛ ♜ ♝ ♞ ♟
🟨
You said:
… run the project it only show me '?'.
I suggest you check the character-encoding setting within the terminal/console app where this text appears.
Sounds like that means the console feature within NetBeans for you, so check the NetBeans preferences/settings to specify UTF-8 for that tool.
In 2024, most every such terminal/console app should default to UTF-8, but unfortunately some do not.
Here is a screenshot of the analogous setting within the IntelliJ IDE. Note the value UTF-8
.
Your console app also needs a font with a glyph defined for each of those characters.
By the way, modern Java offers text blocks to ease the typing of such strings.
You will get the same result. Different syntax, same semantics.
// white king
// white queen
// white rook
// white bishop
// white knight
// white pawn
//
// black king
// black queen
// black rook
// black bishop
// black knight
// black pawn
//
String unicodeMessage = """
\u2654 \
\u2655 \
\u2656 \
\u2657 \
\u2658 \
\u2659 \
\u265A \
\u265B \
\u265C \
\u265D \
\u265E \
\u265F \
""";
IntStream
FYI, you might find an IntStream
handy for a series of consecutive code points.
The 0x
prefix defines a hexadecimal literal in Java.
String chessPiecesWhite =
IntStream
.rangeClosed( 0x2654 , 0x2659 )
.mapToObj( Character :: toString )
.collect( Collectors.joining( ) );
String chessPiecesBlack =
IntStream
.rangeClosed( 0x265A , 0x265F )
.mapToObj( Character :: toString )
.collect( Collectors.joining( ) );
Or define an enum. (I may have had too much coffee today.)
We define the enum objects in the numeric order of their code points. See Wikipedia, Chess symbols in Unicode.
Rather than hard-code each code point, we can let the computer do some math. Our math takes advantage of the fact that the code points are consecutive.
public class Piece
{
public static enum White
{
KING, QUEEN, ROOK, BISHOP, KNIGHT, PAWN;
public String symbol ( )
{
return Character.toString( 0x2654 + this.ordinal( ) ); // Misnomer: `.ordinal` method returns a zero-based index, not a 1-based ordinal.
}
}
public static enum Black
{
KING, QUEEN, ROOK, BISHOP, KNIGHT, PAWN;
public String symbol ( )
{
return Character.toString( 0x265A + this.ordinal( ) ); // Misnomer: `.ordinal` method returns a zero-based index, not a 1-based ordinal.
}
}
}
Usage:
String whiteSymbols = Arrays.stream( Piece.White.values( ) ).map( Piece.White :: symbol ).collect( Collectors.joining( ) );
String blackSymbols = Arrays.stream( Piece.Black.values( ) ).map( Piece.Black :: symbol ).collect( Collectors.joining( ) );
whiteSymbols = ♔♕♖♗♘♙
blackSymbols = ♚♛♜♝♞♟
To recap: Here again we generate text where each character is the symbol for each chess piece. Whether you see an actual glyph depends on two things: