2

I'm getting address using Geocoder. Sometimes I get city or postcode twice. I want to remove these duplicate words to make the address more acceptable. Is there any way to do this? I searched for removing duplicate words from string but only got one good result but it didn't worked in my case. From the below code if I fetch my current address, I get result:

Street no 0360  
DH Block(Newtown), Action Area I, Newtown  
New Town, West Bengal 700156  
New Town  
700156  
India  

Code

    Geocoder geocoder = new Geocoder(this, Locale.getDefault());

        List<Address> addresses = null;
        String result = null;

        try {

            addresses = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);

        } catch (IOException ioException) {
            errorMessage = getString(R.string.service_not_available);
            Log.e(TAG, errorMessage, ioException);
        } catch (IllegalArgumentException illegalArgumentException) {
            errorMessage = getString(R.string.invalid_lat_long_used);
            Log.e(TAG, errorMessage + ". " +"Latitude = " + location.getLatitude() +", Longitude = " + location.getLongitude(), illegalArgumentException);
        }

        if (addresses == null || addresses.size() == 0) {
            if (errorMessage.isEmpty()) {
                errorMessage = getString(R.string.no_address_found);
                Log.e(TAG, errorMessage);
            }
            deliverResultToReceiver(AppUtils.LocationConstants.FAILURE_RESULT, errorMessage, null);
        } else {
            Address address = addresses.get(0);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                sb.append(address.getAddressLine(i)).append(" ");
            }
            sb.append(address.getLocality()).append(" ");
            sb.append(address.getPostalCode()).append(" ");
            sb.append(address.getCountryName());
            result = sb.toString();

       }    
Community
  • 1
  • 1
CodeAssasins
  • 237
  • 5
  • 17
  • Please explain why the linked question did not work. That's exactly how to remove duplicates and preserve order. Add all elements to a LinkedHashSet – OneCricketeer Oct 06 '16 at 08:14
  • I tried but I still got the same result. – CodeAssasins Oct 06 '16 at 08:23
  • It is not clear what you tried. Besides, the only value it should work on is `DH Block(Newtown), Action Area I, Newtown`, but only if you split on spaces and add one after Block, then strip the parenthesis. In other words, I think you didn't understand how to adapt that answer to the code you have. And what is your expected output? – OneCricketeer Oct 06 '16 at 08:29
  • I tried to separate the whole result string `Street no 0360 DH Block(Newtown), Action Area I, Newtown New Town, West Bengal 700156 New Town 700156 India` using space. But I got the same string back without removing the duplicate words. My code is `String ds = new LinkedHashSet (Arrays.asList(result.split(" "))).toString().replaceAll("(^\\[|\\]$)", "").replace(", ", " ");`. Do I need to change something here? – CodeAssasins Oct 06 '16 at 08:50
  • As far as I can tell, that should have removed `New Town 700156` – OneCricketeer Oct 06 '16 at 08:54
  • 2
    You should be careful with this, some addresses might not make sense if you remove duplicates – dahui Oct 06 '16 at 09:00

2 Answers2

0

//call method like

  String Address = "Street no 0360 DH Block(Newtown), Action Area I, NewTown NewTown, West Bengal 700156  NewTown 700156 India";

    String output = RemoveDuplicateWords(Address.toLowerCase());

// Method for remove duplicate words

public String RemoveDuplicateWords(String word){
        int i=0,j=0,n;
        word = word.replaceAll(",", "");
        word = word.replace("(", " ");
        word = word.replace(")", " ");
        String array_word[];
        String final_string="";
        array_word=word.split(" ");
        n=array_word.length;


        for(i=0;i<n;i++){
            for(j=i+1;j<n;j++){
                if(array_word[i].equalsIgnoreCase(array_word[j]) && !array_word[i].equalsIgnoreCase("**")){
                    array_word[j]="**";
                }
            }
        }


        for(i=0;i<n;i++){
            if(array_word[i]!="**")
            {
                final_string=final_string+array_word[i]+" ";
            }
        }

        return final_string;
    }
Rajesh Satvara
  • 3,842
  • 2
  • 30
  • 50
0

The way your are parsing your address isn't extremely robust. You can fix the duplication problem by using an address validation service. It will match the input to entries in a database of real addresses, which would remove duplicate words and standardize the address.

I recommend SmartyStreets. Try it out with your search term on the demo page.

Fair disclosure: I work for SmartyStreets.

Joseph Hansen
  • 12,665
  • 8
  • 50
  • 68