0

I'm having trouble getting the £ sign to show properly in the command prompt after doing:

System.out.println("The price from x to y is £4");

When executed in the command prompt it reads : The price from x to y is ú4

Adam
  • 1
  • 1
  • 1
  • 1
    What is the defualt character encoding for your system? – Peter Lawrey Apr 28 '11 at 14:55
  • http://stackoverflow.com/questions/4426171/setting-encoding-format-to-java-in-shell-script/4426219#4426219 – Matt Ball Apr 28 '11 at 14:56
  • 2
    See http://stackoverflow.com/questions/4747358/java-unicode-confusion/ – axtavt Apr 28 '11 at 15:07
  • Possibly useful reading material: http://www.joelonsoftware.com/articles/Unicode.html – millimoose Aug 10 '13 at 13:26
  • The problem with displaying non-ASCII characters is that in any given situation there's about five places things can go wrong and it's impossible to tell how to solve your problem without determining what went wrong. I.e. whether the problem is that your text editor saves in a different encoding than the compiler inputs, or that your console is using a different encoding than Java is printing, etc. Basically, at every text input/output interface, **both** sides have to use the same encoding, and plain text usually doesn't afford the metadata to specify it. – millimoose Aug 10 '13 at 13:28
  • @ Adam: There is terminal emulator called mintty on windows which handles this cleanly. It would have worked fine on linux – Jayan Aug 10 '13 at 13:31
  • Oh and to add insult to injury, I believe that Windows command prompts still default to a legacy codepage instead of Unicode. (And I'm absolutely not sure whether Java will detect that you've set the console encoding to UTF-8, so you might be hosed if the encoding that it's set to simply doesn't have the pound character.) – millimoose Aug 10 '13 at 13:31

3 Answers3

2

The only variant which works for me:

System.out.println((char) 339);
Dmitri Algazin
  • 3,332
  • 27
  • 30
2

You need to supply a Unicode value in the string for the pound symbol.

You should do:

System.console().writer().println("The price from x to y is \u00A34");

See this page for more information.

See also this question.

Community
  • 1
  • 1
retrodrone
  • 5,850
  • 9
  • 39
  • 65
2

I Windows run

chcp 1252

in your command prompt first.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347