• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to convert Unicode code points to human readable form and print using System.out.println() ?

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to convert Unicode code points to characters and print using System.out.println(). The emojis at https://home.unicode.org/ cannot be printed. When tried to print, a question mark was the output. Does the encoding of System.out.println() have to be changed?

 
Marshal
Posts: 27211
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, unfortunately, yes.

But you can't do that from your Java code. System.out.println writes to the console, which is part of the operating system or runtime environment where you are running it. That may or may not even be possible. So if it's really really important to write emojis to your console, then tell us where you are running your code and somebody might be able to help out. But really I would recommend writing the text to somewhere which supports emojis.
 
Varuna Seneviratna
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just tried out printing Unicode in jshell and, some did work out and some didn't. I wanted to find out how to be able to print any Unicode codepoint to character form or human readable form. This printing was done in the Windows command line.
 
Saloon Keeper
Posts: 13868
314
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that question marks mean that the user interface is using a font that doesn't have glyphs for the printed characters. Sometimes that means you're printing the wrong characters (on account of using a different encoding for printing the characters than the user interface is using for reading them), but sometimes it also just means that your font just doesn't suffice.

In short, you need to assure two things:

  • That the Windows command line is using the same encoding that you are using to print characters.
  • That the Windows command line is using a font that can display the printed characters.
  •  
    Marshal
    Posts: 75669
    354
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Question too difficult for “beginning” forum: moving.

    What happens if you use the SringBuilder.appendCodePoint() method and then print out the StringBuilder object?
     
    Campbell Ritchie
    Marshal
    Posts: 75669
    354
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Also, what happens if you display the StringBuilder, via its toString() method, on a JOptionPane dialogue?
     
    Saloon Keeper
    Posts: 25466
    180
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You shouldn't have to play games at the codepoint level. Java works with Unicode and only when you set up a print stream with code translation (for example, to ISO-8990-1) will there be a coding issue. I'm pretty sure all major command shells (Windows and Linux) are now defaulting to UTF-8, though, and Java likewise.

    On the other hand, your command shell has a specific font assigned to it, and, as Stephan noted, if that font doesn't contain glyph definitions for the Unicode characters you are printing, then the best you'll get is boxes, question-marks, blank spaces or whatever glyph that font has designated for un-renderable character codes.

    So it's really a matter of ensuring that your local system preferences are set up compatibly.

    Aside from that, emojis on console messages is cute only the first few times. Save them for more light-hearted environments.
     
    Varuna Seneviratna
    Ranch Hand
    Posts: 213
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @Tim Holloway,
                     Thanks, Tim.

    So it's really a matter of ensuring that your local system preferences are set up compatibly.



    In fact, what made me play games at the code point level is to understand how encoding and decoding translation works. How to determine whether the local system preferences are set up compatibly?  And how is it possible to determine whether the issue is a coding issue or that the font doesn't contain glyph definitions for the Unicode characters? By reading a Unicode code point how to determine where the code point lies within Unicode? In UTF-8 or UTF-16?

    Java works with Unicode and only when you set up a print stream with code translation (for example, to ISO-8990-1) will there be a coding issue.


    I will try to find out answers to these questions and let you know if I succeed.
     
    Tim Holloway
    Saloon Keeper
    Posts: 25466
    180
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    "Local system preferences" can be very local indeed. In Linux, at least, I can set a different font for each command shell window if I'm using one of the more flexible terminal apps. Windows I'm less sure of, since some of the stuff I used to reference back when I did Windows seems to have gone into hiding (Microsoft's perennial move-stuff-around-gratuitously game, probably).

    An app like MS-WORD of course has its own font selections, including different font selections within individual words.

     
    catch it before it slithers away! Oh wait, it's a tiny ad:
    free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic