Java – use Unicode to display the euro symbol and change the characters to uppercase

I have to use java to achieve this goal

Part 1: output € 188 using character raw data type Use Unicode as the euro symbol €

Part 2: change the following char variable 'J "O" e' to uppercase Joe and output the result

I used this code. What did I miss?

public class Test27 {

    public static void main (String args[]){
    System.out.println("\u20ac" +"188");

    String changeCase= "joe";

    String result;
    result=changeCase.toUpperCase();

    System.out.println( result);        
    }
}

Cheers!

Solution

If the problem is just that the euro logo has become chaotic – it's the process

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, actively must know about Unicode and character sets (no exceptions!)

Then, you need to match the code emitted by java with the code expected to display the Java output I assume you work on the command line

>On Linux, this should work By default, everything is UTF-8. > On MAC, on terminal In the app, this doesn't work because for some crazy reason, the default text encoding of Java is an anchor macroman character set without euro But terminal The app fully supports UTF-8 Technically, you can turn it off in terminal → preferences → settings → advanced → international, but it is UTF-8 by default

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

java -Dfile.encoding=UTF-8 Foo

But this only works if you can control the startup of your program If you want to send a jar or Class file for others to run, it will be invalid You can write to system. Net using a different encoding by creating one Out object to set its own code:

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");
    }
}

As long as you remember to always print with the new out variable instead of system out. > On windows, it becomes more crazy The default encoding at the command prompt varies from language to language version of windows On the English version of windows, it is cp850 On Russian windows, it is cp866 No euro symbol! You can use the CHCP command to change the encoding, but even if you change it to the encoding with euro symbol, the default command prompt font does not have euro symbol!

You can detect from Java that you run from the windows command prompt, programmatically change the encoding and font, and then output your string, but this is a lot of work You'd better only use the above code to force the output of UTF-8, and add instructions to your code. If you want to run from the windows command prompt, users first need to:

>Run CHCP 65001 to switch the command prompt encoding to UTF-8 > switch the font to the lucida console by clicking the icon in the upper left corner, selecting properties, and then going to the Font tab

For your convenience, but to increase the chance that the code you write will only run on your computer, you can also use change the default command prompt code page to UTF-8

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>