有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从地址中删除重复字

我正在使用地理编码器获取地址。有时我会得到两次城市或邮政编码。我想删除这些重复的单词,使地址更容易被接受。有没有办法做到这一点?我试图从字符串中删除重复的单词,但只得到一个好的result,但在我的例子中它不起作用。从下面的代码中,如果我获取当前地址,我将得到结果:

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

代码

    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();

       }    

共 (2) 个答案

  1. # 1 楼答案

    //像这样调用方法

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

    //删除重复单词的方法

    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;
        }
    
  2. # 2 楼答案

    您解析地址的方式不是非常健壮。您可以通过使用address validation服务修复复制问题。它将把输入与真实地址数据库中的条目相匹配,这将删除重复的单词和地址

    我建议SmartyStreets。在demo page上用你的搜索词试试看

    公平披露:我为SmartyStreets工作