2

Whenever I print or write the £ symbol , or even do anything with it, it is always displayed as £ with no reason for the  to be there. I have tried writing the character in unicode but it still resulted in this weird combination. Is there any way to remove this or stop it happening?

public class GenericClass
{
    public static void main(String[] args)
    {
        System.out.println("£");
    }
}

This is all that is printed:

enter image description here

Nitin Bisht
  • 5,053
  • 4
  • 14
  • 26
  • 2
    Where are you printing it? It could be the screen/tool etc can't handle unicode properly. – Bohemian Aug 09 '18 at 18:41
  • Just to the command line, that isn't the issue because it won't work when I just System.out.println() it. – Thomas Rowe Aug 09 '18 at 18:43
  • `public class GenericClass { public static void main(String[] args) { System.out.println("£".substring(1)); } }` What does this produce for you? – Dr3amer17 Aug 09 '18 at 18:49
  • That works well, ill research substring and see how i can apply that to where I need it. Thanks – Thomas Rowe Aug 09 '18 at 18:56
  • 2
    @Dr3amer17 this is one of those cases where I wish StackOverflow allowed downvoting of comments. What you propose is just plain wrong. The fact that it works at all means the `£` is not encoded as expected when compiled. And your proposal fails miserably for Unicode characters that are encoded in UTF-8 in such a way that the value of the 2nd byte does not match the value of the codepoint that is being encoded. The *real* solution is to fix the encoding, not work around it. – Remy Lebeau Aug 12 '18 at 23:54
  • @RemyLebeau I would (and did) flag as Something else > "Dangerously misleading" – GDF Aug 14 '18 at 12:19

1 Answers1

4

Your source file is probably UTF-8 and as such, treats £ as two 8-bit characters : £

For portability, you have to use escaped unicode sequences in your string literals like this.

public class GenericClass
{
    public static void main(String[] args)
    {
        System.out.println("\u00A3");
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
GDF
  • 157
  • 7