javaunicodeutf-8java-8thai

Thai language is not showing in Java output


Unable to print Thai string value in Java console

public static void main(String [] args){
   String engParam = "Beautiful";
   String thaiParam = "สวย";
   System.out.println("Output :" + engParam + ":::" + thaiParam);}

Output is showing like:

Output :Beautiful:::à?ªà??à?¢

I think System.out.println will not be able to print the UTF-8 characters with default console settings. Is there any other way available to resolve this issue? help needed.


Solution

  • You don't specify your environment, but this approach worked for me on Windows 10 from within my IDE, and also from the Command window:

    Here are the steps to get things working:

    These instructions are specific to Windows 10. If you are running in a different environment update your question with full details of your platform and your IDE.


    Updated 12/15/19 to provide an alternative approach:

    Instead of using Code page 874 (Thai) from the Command window, you could do this instead:

    Here's the code:

    package thaicharacters;
    
    import java.io.PrintStream;
    import java.io.UnsupportedEncodingException;
    import java.nio.charset.StandardCharsets;
    
    public class ThaiCharacters {
    
    public static void main(String[] args) throws UnsupportedEncodingException {
    
        String engParam = "Beautiful";
        String thaiParam = "สวย";
    
        // Write the output to a UTF-8 PrintStream:
        PrintStream ps = new PrintStream(System.out, true, StandardCharsets.UTF_8.name());
        ps.println("UTF-8: " + engParam + ":::" + thaiParam);
    }
    }
    

    And here's the output in the Command window, showing that:

    chcp65001