Displaying euro symbol using unicode and changing characters to uppercase [closed]

大兔子大兔子 提交于 2019-12-01 09:33:15
andrewdotn

If the question is just about the Euro sign getting garbled—that is, the program

import java.io.*;

public class Foo {

    public static void main (String args[])
        throws Exception
    {
        System.out.println("\u20ac");
    }
}

Then, first you must read The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!).

Then you need to make the encoding that Java sends out match the encoding expected by the thing displaying Java’s output. I’m assuming you’re working at a command line.

  • On Linux this should just work. Everything is UTF-8 by default.

  • On the Mac, in Terminal.app, this won’t work because for some crazy reason the default text encoding for Java is the ancient MacRoman character set which doesn’t have the Euro. But Terminal.app totally supports UTF-8. Technically you can turn that off in Terminal → Preferences → Settings → Advanced → International, but it’s UTF-8 by default.

    To set java to use UTF-8 output, you can pass the command line argument

    java -Dfile.encoding=UTF-8 Foo
    

    But that only works if you can control the startup of your program. If you’re sending JARs or .class files for others to run, that won’t work. You can set up the encoding yourself by creating an object that will write to System.out with a different encoding:

    import java.io.*;
    
    public class Foo {
    
        public static void main (String args[])
            throws Exception
        {
            PrintWriter out = new PrintWriter(
                new OutputStreamWriter(System.out, "UTF-8"), true);
    
            out.println("\u20ac");
        }
    }
    

    So long as you remember to always use your new out variable for printing instead of System.out.

  • On Windows it gets crazier. The default encoding at the command prompt varies between different language versions of Windows. On the English version of Windows, it’s Cp850. On Russian Windows, it’s Cp866. Neither has the Euro symbol! You can change the encoding with the chcp command, but even if you change it to an encoding that does have the Euro symbol, the default command prompt font doesn’t have the Euro symbol!

    You may be able to detect from Java that you are running at the Windows command prompt, change the encoding and font programmatically, and then output your string, but—that’s a lot of work. You’re probably better off just using the above code to force UTF-8 output, and include instructions with your code that if it is to be run at the Windows command prompt, that the user will first need to:

    1. Run chcp 65001 to switch the command prompt encoding to UTF-8
    2. Switch the font to Lucida Console by clicking the icon in the upper left corner, selecting Properties, and going to the Font tab.

    To make things easier for you, but to increase the chances that the code you write will work on your computer only, you can also change the default command prompt code page to UTF-8.

If the output is garbled, rather than a Euro sign, it's probably a problem with the console where you are running the program. Make sure it's capable of printing € and that the default character encoding for the platform matches the console's character encoding.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!