I have to accomplish this using Java
Part1: Output €188 using the character primitive data type . Use a Unicode for the Euro Symbol €
Part2: Change the following char variables ‘j’’o’’e’ to upper case JOE and output the result.
I've used this code, am I missing something?
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
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 toSystem.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 ofSystem.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:
- Run
chcp 65001
to switch the command prompt encoding to UTF-8 - 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.
- Run
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.
来源:https://stackoverflow.com/questions/17174270/displaying-euro-symbol-using-unicode-and-changing-characters-to-uppercase