0

I have an error in printing special characters in Java.

The system reads a product name from a mysql database, and checking the database from the command line, the data displays the Registered Symbol ® correctly.

A java program then runs a database read to get information of orders to print out as a PDF, but when the print is produced the ® symbol becomes 'fi'.

Is there a way of retaining the myself character coding when handling in Java?

N.B
  • 11
  • 1
  • 1
    There is maybe an encoding problem. Does encoding properties set when connecting to MySQL? Try to look here: https://stackoverflow.com/a/3275661/2530910. Also, could you provide the code? – akvyalkov Mar 30 '21 at 12:05

2 Answers2

0

Before printing to PDF, you can replace the special characters with the unicode characters as below.

public static String specialCharactersConversion( String charString ) {          
     if( isNotEmpty( charString ) ){
          charString = charString.replaceAll(  "\\(R\\)", "\u00AE" );
     } 
}
AbhiN
  • 642
  • 4
  • 18
0

There is an Open Source java library MgntUtils that has a Utility that converts Strings to unicode sequence and vise versa:

result = "Hello World";
result = StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(result);
System.out.println(result);
result = StringUnicodeEncoderDecoder.decodeUnicodeSequenceToString(result);
System.out.println(result);

The output of this code is:

\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064
Hello World

So, what you can do before converting your text to PDF, you can convert special characters or entire text to Unicode sequences. The answer is copied with modifications from this question: Convert International String to \u Codes in java
The library can be found at Maven Central or at Github It comes as maven artifact and with sources and javadoc

Here is javadoc for the class StringUnicodeEncoderDecoder

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36