# comparision between two lists

• 07-18-2008, 07:47 AM
suprabha
comparision between two lists
hi all,
pls suggest me a logic ..we have a text file which contains list of cities with two or more letter missing in city names..and we have to read this using file reader..and it is to compare with the database which contains table with 2 columns.where one column contains normal city names which is taken as key..and another is standard city names..which is taken as values..we are storing it in hash map..den we have to compare list with the hash map one by one and it should return the proper standard city names..we have done the code for proper input..but we could not do it for input with some letters missing..so please provide some suggestion/code....
• 07-18-2008, 07:56 AM
Eranga
Basically you want to compare two strings, which are one is the correct and the other missing some letters.

Is there any pattern of missing letters. If not you have to do more works actually. You have to build a pattern to compare those two strings with all possibilities. Compare character by character and if something is missing, you have test it with all 26 characters again.

I hope it's clear to you.
• 07-18-2008, 09:19 AM
suprabha
there is no pattern for missing letters.....the city name with missing letters should match more than 50% of proper city name which is there in database...
• 07-18-2008, 09:29 AM
jurka
yeah if no pattern then you need compare one city with other full list char by char.
• 07-18-2008, 09:39 AM
Eranga
May be if you think in a grate way, you can come across with your won algorithm to do it in easy way.
• 07-18-2008, 10:14 AM
suprabha
ya we have compared character by character..and we are not geting..i wanted to send you code..but here..can we send code??
• 07-18-2008, 10:27 AM
suprabha
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class MapClass3
{
public static void main(String args[])throws IOException
{
try
{
Connection con;
Ex obj=new Ex();
List ll1=new ArrayList();
ll1=obj.getName();

// System.out.println(ll1);

con=S1.getConnection();
PreparedStatement pst=con.prepareStatement("select * from CITYNAME ");
ResultSet rs=pst.executeQuery();
HashMap hm = new HashMap();
String nc=null;
String sc=null;
while(rs.next())
{
nc=rs.getString("CITY");
sc=rs.getString("STDCITY");
hm.put(nc, sc);
//System.out.print(nc+" ");
//System.out.println(sc);
}
Set set = hm.keySet();
int length;
String temp1=null;
String temp=null;
int match=0;
int notMatch=0;
char[] chr;
char[] chr1;
for (Iterator iterator1 = ll1.iterator();iterator1.hasNext();)
{
temp1 = (String) iterator1.next();
//System.out.println(temp1);
chr1=temp1.toCharArray();
System.out.println(chr1);

for (Iterator iterator = set.iterator(); iterator.hasNext();)
{
temp = (String) iterator.next();
//System.out.println(temp);

chr = temp.toCharArray();
// System.out.println(chr);

if(temp1.length()<temp.length())
{
length=temp1.length();

}
else
{
length=temp.length();

}

for(int i=0;i<length;i++)
{

boolean equals = (chr[i]==chr1[i]);

if(equals!=true)
{

notMatch++;
}
else
{
match++;

}
}

if(notMatch<match)
{
System.out.println(hm.get(temp1));

}

}

/* if(temp.equals(temp1))
{

{
System.out.println("standard city name:"+hm.get(temp1));

}
}*/

}

}catch(Exception e)
{
ExceptionClass g=new ExceptionClass();
g.displayException(e);
}
}
}
• 07-18-2008, 10:28 AM
Eranga
Sure, send the code you have done upto now. We can look at and give an idea.
• 07-18-2008, 10:29 AM
suprabha
i have sent the code..here we are comparing one by one with the list which is in the database
• 07-18-2008, 01:40 PM
skaspersen
You could use a spell checker library, eg Jazzy.

You would then create a dictionary with only the proper names of the Cities in it from the Database.

Ask the dictionary for suggestions for each item in the list if it only returns a single result use that as the key to get the value from the Hashmap.

If this is for an assignment the algorithms you need to look up are Phonetic matching Algorithms and The Levenshtein algorithm

HTH
Stephen
• 07-21-2008, 06:34 AM
suprabha
but it should compared with the given wrong city name right..where it will compare...?can you suggest me some source code
• 07-21-2008, 01:42 PM
skaspersen
Code:

```SpellDictionary dictionary = new SpellDictionaryHashMap(); HashMap<String,String> hm = new HashMap<String,String>(); //TODO add city names to dictionary using dictionary.addWord(String); and to hm public String getStandardCityName(String improperName){     List possible = dictionary.getSuggestions(improperName,3);     if(possible.size()==1){         return hm.get(possible.get(0));     }     return null; }```
• 07-22-2008, 07:37 AM
suprabha
thanks 4 the code..we will try it
• 07-29-2008, 07:35 AM
jack239
Suprabha plz let us know if it works or not and if you modify anything to make it work then post the sample code for us.
• 08-01-2008, 02:49 PM
suprabha
we have not tested that code..after testing i will surely send you back.thanks.