9

What I want as an end result is this

System.out.println("This is the not equal to sign\n≠");

to appear (when run) as

This is the not equal to sign
≠

not to appear as

This is the not equal to sign
?

Is there any way to do this? I tried using windows character map, copied the symbol here, and in my code, but after changing encoding to UTF-8 and inserting it, it comes up as ? when run...
What can be done? Thanks in advance for answers to this utterly simple question

jmj
  • 237,923
  • 42
  • 401
  • 438
Kyle
  • 299
  • 1
  • 2
  • 11
  • 5
    Seems like a problem with encoding of your console, not Java program. http://ideone.com/OJhKeG - works fine – Crozin Apr 01 '14 at 17:31
  • 3
    It's probably your console. But to make sure it is not the file encoding that's at fault, you can use the Unicode escape `\u2260` instead of the `≠` character in the string. – Tobias Brandt Apr 01 '14 at 17:32
  • I agree with Crozin. You can test it by writing to a file instead of stdout, and then opening the file in an editor you know can handle unicode. If that works, it means Java's handling the bytes correctly, and the problem must be your console. – yshavit Apr 01 '14 at 17:32
  • Working for me. try using notepad. hope you are using IDE. – Lasitha Benaragama Apr 01 '14 at 17:35

5 Answers5

6

Set character encoding to UTF-8, pass this vm argument, if your text editor already uses UTF-8 or supports this character

-Dfile.encoding=UTF-8
jmj
  • 237,923
  • 42
  • 401
  • 438
4

As @Tobias Brandt says, you could use: \u2260

And btw also @Crozin is right about your console configuration

Like this

System.out.println("This is the not equal to sign \n\u2260");
Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
3

There are five potential issues here:

1) In which charset encoding are you saving (from your editor) you Java source?

2) Which charset encoding the java compiler assumes?

3) Which charset is your console?

4) Are you using some terminal with translation?

5) Does your console font include that particular character?

For getting issues 1-2 right, you should use UTF-8 for both (editor and javac settings), or more robust, specifify the Unicode char with escaped pure ascii text (Frakcool answer).

For issue 3, try -Dfile.encoding=UTF-8 or see this answer. Issues 4-5 are outside your Java program scope. If you are unsure, just redirect the ouput to a file, and look at it with a Hex editor.

Community
  • 1
  • 1
leonbloy
  • 73,180
  • 20
  • 142
  • 190
0

When you save the java file, make sure it is saved in the same Charset as the one it is open.

In my Eclipse, when I save a file with special chars (such as \u2260) it asks me what charset I want to use.

Open your file in the terminal and inspect the content of the file.

Make sure it is the same char as the one in the editor you are using.

Alexandre Santos
  • 8,170
  • 10
  • 42
  • 64
0

It seems that after Eclipse asked me if I want to change to UTF-8, it worked, only after I posted this. Sorry for wasting your time

Kyle
  • 299
  • 1
  • 2
  • 11