-1

I am developing a card game in Java and I am currently using a command line to interact and display outputs of the game. I have been representing each card suit with A letter (H - Hearts, S - Spades etc.)

I came up with the idea of using the Unicode values rather than letters to show each suit

public String toSymbol(Suit suit){
    switch(suit){
        case SPADE:
            return "\u2664";
        case DIAMOND:
            return "\u2662";
        case CLUB:
            return "\u2667";
        case HEART:
            return "\u2661";
        default:
            return "null";
    }
}

My issue is that when trying to print these symbols they simply display our favourite Unknown Character '?'. I read that this could be due to the byte stream System.out.println() uses something too short? Instead I should use a Print Writer.

I tried this and still no results. Could it be to do with the format my commandline uses?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 3
    Which OS and terminal emulator are you using? – Marco Balo May 09 '22 at 22:33
  • 6
    The characters are being written correctly by your code. The problem is the terminal in which the output is being displayed is probably not configured for UTF8, and/or the font you're using doesn't contain those characters. – Jim Garrison May 09 '22 at 22:46
  • If you want to guarantee that it is displayed correctly on any PC then you need to use a GUI (Swing/JavaFX) so that you have full control of the font and encoding/characterset. However, if you are using windows command prompt to start your application then first use `chcp 65001` in the console to change the character set to one that hopefully supports your characters, then launch your app, but that is still no guarantee that it will work. See here for some more details: https://stackoverflow.com/questions/67085339/java-with-maven-netbeans – sorifiend May 10 '22 at 01:33
  • @JimGarrison An additional possibility is that the **PrintStream** used by **System.out** is not configured to use UTF-8 encoding. It may not be on Windows, by default. Using an appropriate font, and configuring the console for UTF-8 are both necessary, but ensuring that the **PrintStream** is using the proper encoding also matters. – skomisa May 10 '22 at 06:06
  • 1
    More information is needed. Update your question with all of the following: Your O/S, the version of Java you are using, the font you are using, and the form of terminal/console you are using (e.g Linux terminal, Windows command line, IDE console, etc.). – skomisa May 10 '22 at 06:09

1 Answers1

2

Glyph

As commented, you should check that your System.out and console are set to UTF-8, and that a font is available containing glyphs for those characters.

You neglected to mention your operating system, but I’ll guess it is Microsoft Windows. If so, this Question might help: Java, UTF-8, and Windows console.

We can simplify your code. Java source code supports UTF-8, so you can use the actual suit characters directly rather then use the Unicode code point escapes.

Here are the four French-suited characters, in “black” and in “white”.

System.out.println( "♠️♥️♣️♦️" ) ; 
System.out.println( "♤♡♧♢" ) ; 

See this code run successfully at Ideone.com.

Enum

By the way, you can embed knowledge of the emoji character within your enum. No need to maintain this separate method as shown in your Question.

enum Suit {
    SPADES( "♠️" , "♤" ), HEARTS( "♥️" , "♡" ), CLUBS( "♣️" , "♧" ), DIAMONDS( "♦️" , "♢" );
    private final String emojiBlack, emojiWhite ;
    Suit( String emojiBlack , String emojiWhite ) {  // Constructor.
        this.emojiBlack = emojiBlack ;
        this.emojiWhite= emojiWhite ;
    }
    String emojiBlack() {  // Accessor (getter).
        return this.emojiBlack ;
    }
    String emojiWhite() {  // Accessor (getter).
        return this.emojiWhite ;
    }
}

Usage:

System.out.println(
    Suit.HEARTS.emojiBlack() 
);

♥️

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154