Вопрос:

How to use Special Chars in Java/Eclipse

java eclipse unicode encoding cp1252

71299 просмотра

7 ответа

11886 Репутация автора

How can I use/display characters like ♥, ♦, ♣, or ♠ in Java/Eclipse?

When I try to use them directly, e.g. in the source code, Eclipse cannot save the file.

What can I do?

Edit: How can I find the unicode escape sequence?

Автор: Burkhard Источник Размещён: 14.10.2008 10:42

Ответы (7)


2 плюса

1130666 Репутация автора

Either change your encoding to one which will cope, e.g. UTF-8, or find the relevant Unicode number and use a \uxxxx escape sequence to represent it.

Автор: Jon Skeet Размещён: 14.10.2008 10:45

36 плюса

6961 Репутация автора

Решение

The problem is that the characters you are using cannot be represented in the encoding you have the file set to (Cp1252). The way I see it, you essentially have two options:

Option 1. Change the encoding. According to IBM, you should set the encoding to UTF-8. I believe this would solve your problem.

  • Set the global text file encoding preference Workbench > Editors to "UTF-8".
  • If an encoding other than UTF-8 is required, set the encoding on the individual file rather than using the global preference setting. To do this use the File > Properties > Info menu selection to set the encoding on an individual file.

Option 2. Remove the characters which are not supported by the "Cp1252" character encoding. You can replace the unsupported characters with Unicode escape sequences (\uxxxx). While this would allow you to save your file, it is not necessarily the best solution.

For the characters you specified in your question here are the Unicode escape sequences:

♥ \u2665
♦ \u2666
♣ \u2663
♠ \u2660
Автор: Joe Lencioni Размещён: 14.10.2008 10:49

9 плюса

6314 Репутация автора

It can be solved by setting encoding in eclipse:

1st way:

At the menu select File-->Properties and then at the "Text file encoding" section: Select Other radio, Select UTF-8 from combo -> Lastly click OK button

2nd way:

Right click on specific file (say Test.java) -> Properties. In Text file encoding section: Select Other radio, Select UTF-8 from combo -> Lastly click OK button

3rd way:

If you want to make this change for all your project go at Window-->Preferences--> General--> Workspace . In Text file encoding section: Select Other radio, Select UTF-8 from combo -> Lastly click OK button

Автор: idrosid Размещён: 14.10.2008 10:56

2 плюса

138580 Репутация автора

Finding the unicode escape sequence: see these Unicode charts. Your characters are in the Misc. Symbols chart, \u2660 and up.

Автор: MSalters Размещён: 14.10.2008 11:37

17 плюса

10614 Репутация автора

In Eclipse:

  1. Go to Window -> Preferences -> General -> Workspace -> TextFileEncoding
  2. Set it to UTF-8
Автор: sepehr Размещён: 12.02.2009 08:13

0 плюса

21 Репутация автора

Windows Menu –> Preferences –> General (expand it) –> Workspace (click on it). Look for a box “Text File Encoding”. Default will be “Cp1252″. Change radio to select other and select “UTF-8″ from combo box.

Автор: Sonu Размещён: 21.06.2013 10:17

2 плюса

7128 Репутация автора

Expanding a bit on @Joe Lencioni answer

You can use AnyEdit Eclipse plugin (install-able from Eclipse marketplace) to easily convert Unicode text into Java Unicode escapes:

  • Select char/text with non-ASCII characters
  • right-click
  • Convert > To Unicode Notation

One minor caveat is that AnyEdit wants to save the file first which obviously is disallowed by Eclipse until you fix your text.

Автор: Piotr Findeisen Размещён: 29.10.2013 09:50
Вопросы из категории :
32x32