Search code examples
javastringcharacter-encoding

unmappable character (0xC2) for encoding US-ASCII


I am practicing on of methods of ArrayList set().I got very crazy result here: enter image description here How to escape this error and successfully to set new value for ArrayList?

I expected this output : [Forever Garcinia Plus®, Gel Aloe Vera, Hello, Forever Viber]Hello, World! , as after word Plust it should be symbol R.

Actually resulted this one:

/tmp/SrUvRBAjDt/HelloWorld.java:12: error: unmappable character (0xC2) for encoding US-ASCII
        list.set(0,"Forever Garcinia Plus??");
                                         ^
/tmp/SrUvRBAjDt/HelloWorld.java:12: error: unmappable character (0xAE) for encoding US-ASCII
        list.set(0,"Forever Garcinia Plus??");
                                      ^

2 errors


Solution

  • To place a Unicode character within a String, you can use it's decimal or hexadecimal code, and cast it to a char.

    The Registered trademark symbol is code U+00AE.
    https://en.wikipedia.org/wiki/Registered_trademark_symbol

    list.set(0, "Forever Garcinia Plus" + (char) 0xae);
    

    Additionally, you can use the \u escape sequence, for a char literal.
    Note, a \u escape sequence requires 4 hexadecimal values, 0 padded.

    list.set(0, "Forever Garcinia Plus" + '\u00ae');
    

    Which, ultimately can be reduced to

    list.set(0, "Forever Garcinia Plus\u00ae");