-4

I'm developing a Converter app.
The following lines give me a syntax error called Invalid character constant.

I don't know how to deal with it.
Any kind of help or suggestion are welcome.

public static HashMap<Character, String> UNICODE_TRANS = new HashMap<Character, String>();
static {
    UNICODE_TRANS.put('÷', "/");
    UNICODE_TRANS.put('×', "*");
    UNICODE_TRANS.put('÷', "/");
    UNICODE_TRANS.put('×', "*");
    UNICODE_TRANS.put('²', "^2");
    UNICODE_TRANS.put('³', "^3");
    UNICODE_TRANS.put('�', "^4");
    UNICODE_TRANS.put('−', "-");
    UNICODE_TRANS.put('µ', "micro");
    UNICODE_TRANS.put('Ï€', "pi");
    UNICODE_TRANS.put('Π', "pi");
    UNICODE_TRANS.put('€', "euro");
    UNICODE_TRANS.put('Â¥', "japanyen");
    UNICODE_TRANS.put('₤', "greatbritainpound");
    UNICODE_TRANS.put('√', "sqrt");
    UNICODE_TRANS.put('∛', "cuberoot");
    UNICODE_TRANS.put('½', "1|2");
    UNICODE_TRANS.put('â…“', "1|3");
    UNICODE_TRANS.put('â…”', "2|3");
    UNICODE_TRANS.put('¼', "1|4");
    UNICODE_TRANS.put('â…•', "1|5");
    UNICODE_TRANS.put('â…–', "2|5");
    UNICODE_TRANS.put('â…—', "3|5");
    UNICODE_TRANS.put('â…™', "1|6");
    UNICODE_TRANS.put('â…›', "1|8");
    UNICODE_TRANS.put('⅜', "3|8");
    UNICODE_TRANS.put('â…�', "5|8");
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Kasim Rangwala
  • 1,765
  • 2
  • 23
  • 44

3 Answers3

2

I can see that you have a lot of char constants that contain multiple characters. A char constat must contain one and only one character. That's why your code failed to compile.

You can solve this by deleting the extra characters. But in your comments, I think you implied that you want to keep the meaning of the constants. i.e. You don't want to change 'Ï€' to 'Ï' or '€' because it would lose its meaning.

So for an alternative approach, Use a Map<String, String> instead of a Map<Character, String>. This way, you are ale to store multiple characters!

Sweeper
  • 213,210
  • 22
  • 193
  • 313
1

This must be a character encoding issue. You opened an UTF-8 encoded file with ISO-latin-1 encoding. Change the editor encoding in Eclipse. Here is an answer how to do that.

For a single file, right-click the file, open Properties, and under Resource you can change Text File Encoding to other (and you can choose UTF-8).

Also, you might want to change the character encoding for the compiler, too. This can be done under Project Compiler settings. Right-click on the Project, choose Properties, you will see Builders or Compilers there. Then set the input file encoding.

Community
  • 1
  • 1
gaborsch
  • 15,408
  • 6
  • 37
  • 48
0

Sorry, I'm giving the answer of my own question.
I've spoiled my half day because of Eclipse IDE. Eclipse doesn't open UTF-8 file correctly.
When I opened the same file containing above code in simple Notepad++ editor. It looks like following.

public static HashMap<Character, String> UNICODE_TRANS = new HashMap<Character, String>();
static {
    UNICODE_TRANS.put('÷', "/");
    UNICODE_TRANS.put('×', "*");
    UNICODE_TRANS.put('÷', "/");
    UNICODE_TRANS.put('×', "*");
    UNICODE_TRANS.put('²', "^2");
    UNICODE_TRANS.put('³', "^3");
    UNICODE_TRANS.put('⁴', "^4");
    UNICODE_TRANS.put('−', "-");
    UNICODE_TRANS.put('µ', "micro");
    UNICODE_TRANS.put('π', "pi");
    UNICODE_TRANS.put('Π', "pi");
    UNICODE_TRANS.put('€', "euro");
    UNICODE_TRANS.put('¥', "japanyen");
    UNICODE_TRANS.put('₤', "greatbritainpound");
    UNICODE_TRANS.put('√', "sqrt");
    UNICODE_TRANS.put('∛', "cuberoot");
    UNICODE_TRANS.put('½', "1|2");
    UNICODE_TRANS.put('⅓', "1|3");
    UNICODE_TRANS.put('⅔', "2|3");
    UNICODE_TRANS.put('¼', "1|4");
    UNICODE_TRANS.put('⅕', "1|5");
    UNICODE_TRANS.put('⅖', "2|5");
    UNICODE_TRANS.put('⅗', "3|5");
    UNICODE_TRANS.put('⅙', "1|6");
    UNICODE_TRANS.put('⅛', "1|8");
    UNICODE_TRANS.put('⅜', "3|8");
    UNICODE_TRANS.put('⅝', "5|8");
}

These will might help everyone who uses Eclipse IDE to keep these in Mind.

Here's the Eclipse Comptible Code

public static HashMap<Character, String> UNICODE_TRANS = new HashMap<Character, String>();
static {
    UNICODE_TRANS.put('\u00F7', "/");
    UNICODE_TRANS.put('\u00D7', "*");
    UNICODE_TRANS.put('\u00F7', "/");
    UNICODE_TRANS.put('\u00D7', "*");
    UNICODE_TRANS.put('\u00B2', "^2");
    UNICODE_TRANS.put('\u00B3', "^3");
    UNICODE_TRANS.put('\u2074', "^4");
    UNICODE_TRANS.put('\u2212', "-");
    UNICODE_TRANS.put('\u00B5', "micro");
    UNICODE_TRANS.put('\u03C0', "pi");
    UNICODE_TRANS.put('\u03A0', "pi");
    UNICODE_TRANS.put('\u20AC', "euro");
    UNICODE_TRANS.put('\u00A5', "japanyen");
    UNICODE_TRANS.put('\u00A3', "greatbritainpound");
    UNICODE_TRANS.put('\u221A', "sqrt");
    UNICODE_TRANS.put('\u221B', "cuberoot");
    UNICODE_TRANS.put('\u00BD', "1|2");
    UNICODE_TRANS.put('\u2153', "1|3");
    UNICODE_TRANS.put('\u2154', "2|3");
    UNICODE_TRANS.put('\u00BC', "1|4");
    UNICODE_TRANS.put('\u2155', "1|5");
    UNICODE_TRANS.put('\u2156', "2|5");
    UNICODE_TRANS.put('\u2157', "3|5");
    UNICODE_TRANS.put('\u2159', "1|6");
    UNICODE_TRANS.put('\u215B', "1|8");
    UNICODE_TRANS.put('\u215C', "3|8");
    UNICODE_TRANS.put('\u215D', "5|8");
}
Kasim Rangwala
  • 1,765
  • 2
  • 23
  • 44
  • 2
    Eclipse is able to open UTF-8 files. It does help if you check the file encoding with other tools like Notepad++, but you can properly edit files in Eclipse after applying the character code setting. – gaborsch Oct 30 '15 at 10:04
  • Please go to Eclipse's `Preferences, General/Workspace/Text File Encoding` and set it to **UTF-8** – Phantômaxx Oct 30 '15 at 10:14
  • Another reason to use Android Studio and dump Eclipse. – weston Oct 30 '15 at 10:44