Question

I have read several posts on both Stack Exchange and other sites, but am still a bit stuck on how I can go about printing the Astrological Signs in Java.

public static void main(String[] args)
{
    char aa = \u2648;
    char ab = \u2649;
    char ac = \u264A;
    char ad = \u264B;
    char ae = \u264C;
    char af = \u264D;
    char ag = \u264E;
    char ah = \u264F;
    char ai = \u2650;
    char aj = \u2651;
    char ak = \u2652;
    char al = \u2653;

    while(true)
    {
        System.out.println(aa + ab + ac + ad + ae + af + ag + ah + ai + aj + ak + al);
    }
}

From what I understand, one can only use \u when the hex value value of the unicode is inside of the range U+0000 to U+FFFF, which this is. I tried compiling this and it gives me 12 illegal character syntax errors. Any help would be greatly appreciated. ^_^

Additional Info: I am printing this to the console and I am using TextPad to compile.

Was it helpful?

Solution

Try this. Note that you are printing the chars in an infinite loop.

Update: To print the unicode characters, you will have to create a new output stream using "UTF-8" encoding. Try this. It should work.

public static void main(String[] args) {
    char aa = '\u2648';
    char ab = '\u2649';
    char ac = '\u264A';
    char ad = '\u264B';
    char ae = '\u264C';
    char af = '\u264D';
    char ag = '\u264E';
    char ah = '\u264F';
    char ai = '\u2650';
    char aj = '\u2651';
    char ak = '\u2652';
    char al = '\u2653';

    PrintStream out = null;
    try {
        out = new PrintStream(System.out, true, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    out.println("" + aa + ab + ac + ad + ae + af + ag + ah + ai + aj + ak + al);
}

OTHER TIPS

Use the '' around the char literals to make it compilable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top