Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 988
Dan Wyer Avatar asked Jan 13 '23 02:01

Dan Wyer


2 Answers

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 ridiculous reason the default text encoding for Java is the ancient MacRoman character set [Update: this shouldn’t still be a problem, I believe the default was fixed starting with Java 7] 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.

enter image description hereenter image description here

like image 120
andrewdotn Avatar answered Jan 30 '23 09:01

andrewdotn


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.

like image 33
erickson Avatar answered Jan 30 '23 10:01

erickson