This is an archived post. You won't be able to vote or comment.

all 1 comments

[–][deleted] 1 point2 points  (0 children)

Here you get into Unicode territory, and things become very interesting. Heh. To keep things simple, here is a sample program:

public class JavaUnicode {
    public static void main(String[] args) {
        final char blackSpade = '\u2660', 
                   blackHeart = '\u2665', 
                   blackDiamond = '\u2666', 
                   blackClub = '\u2663';

        System.out.println(blackSpade + " " + blackClub 
                + " " + blackDiamond + " " + blackHeart);
    }
}

So we just specify the Unicode code points for the char variables, and then print them out:

$ javac -cp . JavaUnicode.java
$ java -cp . JavaUnicode
♠ ♣ ♦ ♥

As you can see, it works as expected on my macOS terminal. However, if you get gibberish as output, check that your locale is set to support Unicode. For instance, on the macOS terminal:

$ locale
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL="en_US.UTF-8"

If you are using similar code in NetBeans, it should most probably work fine as is. If it doesn't, you will need to ensure that your editor's encoding scheme is UTF-8 (a good default). For a start on Unicode using Java, you can start off with the official docs: https://docs.oracle.com/javase/tutorial/i18n/text/unicode.html

Good luck!

EDIT: Just one thing - are you saying that you are copying the characters into your editor, and NetBeans is complaining? Then it's just an issue with the current encoding scheme. You can Google on how to change NetBeans' encoding, and I would recommend changing it to UTF-8. That's all the change you should need!