2

I am having a string 'Montaño' and I am using it as key in java Map. But when I am trying to retrieve value for this key. It gives null.

Any Idea what is wrong here?

EDITED : The value that I am getting from DB is shown in the logs Monta�o and is used as Key. But when I am reading the same name from some other parameters ( that also displays name from db ) it shows as Montaño. I am sure of this thing that '�' is ñ. So, what should I do before setting it as key in map. So that I can retrieve the value properly

Ravi.Kumar
  • 761
  • 2
  • 12
  • 34

7 Answers7

2

The "replacement character" � that you get from the database is not ñ, it's a special character used to signal characters that couldn't be correctly decoded. You should fix your database so that it returns Montaño always.

The problem may be with how you connect to the database, not with the individual record. You may need to set the encoding for the connection in the connection string or by setting a property on the connection after obtaining it. You don't say which database you use so I can't more detailed, check the manual for your DB for connection options.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • Well, When same record is displayed on the browser with same character. It is displayed as ñ. So, I don't think this could be problem. But I will surely check. Thanks – Ravi.Kumar Jul 12 '13 at 07:39
0

Your problem is somewhere else, not in Map. Try this test

    HashMap m = new HashMap();
    m.put("Montaño", "Montaño");
    System.out.println(m.get("Montaño"));

output

Montaño
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

I tried

String val = "Montaño";

Map<String, String> map = new HashMap<String, String>();
map.put(val, "test");

System.out.println(map.get(val));

And the output as desired:

test

So might be issue is with the string you trying to get the element. Check whether both the string are same or not.

Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
0

Make sure your table's column accepts the particular character encoding , say UTF-8.

Connect to the DB , using the below flags :

 useUnicode=true;characterEncoding=UTF-8
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
0

This is clearly an encoding issue.

The ñ character is represented in UTF-8 with 2 bytes: C3 B1. In ISO-8859-1, for instance, it is represented with a single byte F1. The fact you're seeing a character seems to be indicating you're interpreting F1 in UTF-8. If you were interpreting the C3 B1 bytes as ISO-8859, you'd see ñ instead.

If you're positive the value is stored correctly on the DB, make sure your JVM is using a compatible encoding (for instance, ISO-8859-1).

The JVM will use by default the platform's encoding, (i.e. Windows-1252 for Windows, UTF-8 for GNU/Linux). Check your DB encoding is compatible, or specify a compatible one for your JVM (UTF is always a good option to avoid these kind of problems).

Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161
0

I think your database is properly encoded. Try passing -Dfile.encoding=UTF-8 at the start of JVM. If it is a standalone program pass it like java -Dfile.encoding=UTF-8 … com.packages.YourMainClass. If it is a web application add it to your server startup script.

Dhrubajyoti Gogoi
  • 1,265
  • 10
  • 18
0

As I do not have access to Server, neither database. The records that I am getting are not directly from database. I am getting all records through java API. So, I finally made a workaround to use instance of that Object ( representing record) as map Key instead of using name as Key. This way my problem is solved.

Thank you all for your concern. Your suggestions have gave me some good ways that I will do on my end.

Ravi.Kumar
  • 761
  • 2
  • 12
  • 34