Print unicode character in java
Asked Answered
P

5

13

Displaying unicode character in java shows "?" sign. For example, i tried to print "अ". Its unicode Number is U+0905 and html representation is "अ". The below codes prints "?" instead of unicode character.

char aa = '\u0905';
String myString = aa + " result" ;
System.out.println(myString); // displays "? result"

Is there a way to display unicode character directly from unicode itself without using unicode numbers? i.e "अ" is saved in file now display the file in jsp.

Picker answered 3/7, 2017 at 5:46 Comment(5)
May be your console isn't able or configured to show that character?Dupion
@Dupion I have tried this in servlet get method. Here also it shows "?" in browser.Picker
is your request returning utf8?Infamous
If you save your .java or .jsp file in utf-8 then you can use unicode characters in it without encodingRosemaryrosemond
You are storing your Unicode in a var of type "char." Put it inside double quotes.Delegate
L
7

Java defines two types of streams, byte and character.

The main reason why System.out.println() can't show Unicode characters is that System.out.println() is a byte stream that deal with only the low-order eight bits of character which is 16-bits.

In order to deal with Unicode characters(16-bit Unicode character), you have to use character based stream i.e. PrintWriter.

PrintWriter supports the print( ) and println( ) methods. Thus, you can use these methods in the same way as you used them with System.out.

PrintWriter printWriter = new PrintWriter(System.out,true);
char aa = '\u0905';
printWriter.println("aa = " + aa);
Lovins answered 5/8, 2018 at 21:20 Comment(0)
B
3

Your myString variable contains the perfectly correct value. The problem must be the output from System.out.println(myString) which has to send some bytes to some output to show the glyphs that you want to see.

System.out is a PrintStream using the "platform default encoding" to convert characters to byte sequences - maybe your platform doesn't support that character. E.g. on my Windows 7 computer in Germany, the default encoding is CP1252, and there's no byte sequence in this encoding that corresponds to your character.

Or maybe the encoding is correct, but simply the font that creates graphical glyphs from characters doesn't have that charater.

If you are sending your output to a Windows CMD.EXE window, then maybe both reasons apply.

But be assured, your string is correct, and if you send it to a destination that can handle it (e.g. a Swing JTextField), it'll show up correctly.

Bilbao answered 3/7, 2017 at 15:20 Comment(0)
A
2

try to use utf8 character set -

        Charset utf8 = Charset.forName("UTF-8");
        Charset def = Charset.defaultCharset();

        String charToPrint = "u0905";

        byte[] bytes = charToPrint.getBytes("UTF-8");
        String message = new String(bytes , def.name());

        PrintStream printStream = new PrintStream(System.out, true, utf8.name());
        printStream.println(message); // should print your character
Aenneea answered 3/7, 2017 at 5:58 Comment(0)
A
1

I ran into the same problem wiht Eclipse. I solved my problem by switching the Encoding format for the console from ISO-8859-1 to UTF-8. You can do in the Run/Run Configurations/Common menu.

https://eclipsesource.com/blogs/2013/02/21/pro-tip-unicode-characters-in-the-eclipse-console/

An answered 18/2, 2021 at 8:14 Comment(0)
F
1

Unicode is a unique code which is used to print any character or symbol.

You can use unicode from --> https://unicode-table.com/en/

Below is an example for printing a symbol in Java.

        package Basics;
        
        /**
         *
         * @author shelc
         */
        public class StringUnicode {
        
            public static void main(String[] args) {
        
                String var1 = "Cyntia";
                String var2 = new String(" is my daughter!");
        
                System.out.println(var1 + " \u263A" + var2);
    
                //printing heart using unicode
                System.out.println("Hello World \u2665");
        
            }
        
        }
        
    ******************************************************************
    OUTPUT-->
    
    Cyntia ☺ is my daughter!
    Hello World ♥
          
Frear answered 30/5, 2022 at 2:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.