5

Sorry for asking a stupid question, We are trying to print heart symbol from database to Java XML file. But the same is getting printed as "?" not sure where am I missing. Have tried the char unicode. As a practice I tried it using in main method. Please find the sample java class.

public static void main(String[] args) {

    String t = "\u2665";
    String myUnicodeSymbol = "\u05D0";
    char hollowHeart = '\u2661';
    String hollowDiamond = "\u2662";
    String spade = "\u2660";
    String club = "\u2663";

    StringBuffer buffer = new StringBuffer("<HEAD>");
    buffer.append("<HEART>").append(hollowHeart).append("</HEART>");
    buffer.append("</HEAD>");
    System.out.println(t);
    System.out.println(buffer.toString());
}

The ouput is:- ? ?

Not sure what am I missing.

talnicolas
  • 13,885
  • 7
  • 36
  • 56
Kiran
  • 183
  • 5
  • 19
  • Just out of curiosity, why is hollowHeart the only constant represented by a char when all the others are Strings? Have you tried representing it as a string? What have you tried, and what were the effects? – Edwin Buck Feb 01 '12 at 17:19
  • I have experienced similar problem in the past. I created a program which wrote into XML file. If the program was started from Eclipse IDE the content of the XML file was as expected but if it was started from windows console it had a lot of "?" symbols. How do you write your XML? – ka3ak Feb 01 '12 at 17:36
  • Had tried various ways previoulsy tried with String then for testing changed to char – Kiran Feb 02 '12 at 04:22

2 Answers2

7

I think it depends on settings of the console where the output lands.

I have compiled and run your code in Eclipse and have seen heart symbol. But if I run the program from standard windows console then I see "?" instead of heart symbol too.

However you need to change standard settings in Eclipse.

Window -> Preferences -> General -> Workspace -> set "Text file encoding" to "Other: UTF-8"
ka3ak
  • 2,435
  • 2
  • 30
  • 57
0

System.out.println does not support I think the Unicode characters in MAC.

Reference From MACWORLD

Try this

PrintStream out = new PrintStream(System.out, true, "UTF-8");

out.println(t);

You also will need to throw UnsupportedEncodingException or handle it.

Community
  • 1
  • 1
S.P.
  • 3,054
  • 1
  • 19
  • 17
  • No luck yet, I tried the same, in the eclipse workbench, please find the output :-? ? â™ The code is:- public static void main(String[] args) { String t = "\u2665"; String myUnicodeSymbol = "\u05D0"; String hollowHeart = "\u2661"; String hollowDiamond = "\u2662"; String spade = "\u2660"; String club = "\u2663"; PrintStream out; try { out = new PrintStream(System.out, true, "UTF-8"); out.println(spade); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } – Kiran Feb 02 '12 at 04:26