将地址行分隔为House Number,Street name和Apartment in Java或COBOL

时间:2014-04-09 20:50:31

标签: java sql street-address

我目前正在尝试找出一条地址线的最佳方法,并将其分为三个字段,分别为文件,门牌号码,街道名称和公寓号码。值得庆幸的是,城市,州和邮政编码已经在列中,因此我需要解析的只是上面列出的三件事,但即使这样也很难实现。我最初的希望是使用SQL在COBOL中执行此操作,但我不认为我能够使用别人在单独的问题线程上列出的PATINDEX示例,我一直得到-440个SQL代码。我的第二个想法是在Java中使用字符串作为数组并检查数组中的数字,然后检查字母,然后比较“Apt”或类似的效果。到目前为止,我已经尝试过测试我最终要做的事情了,但是我对这个数组的界限超出了范围。

class AddressTest{
    public static void main (String[] arguments){
       String adr1 = "100 village rest court";
       String adr2 = "1000 Arbor lane Apt. 21-D";
       String[] HouseNbr = new String[9];
       String[] Street = new String[20];
       String[] Apt = new String[5];

       for(int i = 0; i < adr1.length();i++){
           String[] forloop = new String[] {adr1};
           if (forloop[i].substring(0,1).matches("[0-9]")){
               if(forloop[i+1].substring(0,1).matches("[0-9]")){
                   HouseNbr[i] = forloop[i];
               }
               else if(forloop[i+1].substring(0,1).matches(" ")){
               }
               else if(forloop[i].substring(0,1).matches(" ")){
               }
               else{
                   Street[i] = forloop[i];
               }
           }
       }

       for(int j = 0; j < HouseNbr.length; j++){
               System.out.println(HouseNbr[j]);
       }
       for(int k = 0; k < Street.length; k++){
           System.out.println(Street[k]);
       }
    }   
}

任何其他想法都会非常有用。

3 个答案:

答案 0 :(得分:1)

我会考虑删除不必要的数组并使用StringTokenizer ......

public static void main(String[] args) {

     String number;
     String address;
     String aptNumber;


    String str = "This is String , split by StringTokenizer";
    StringTokenizer st = new StringTokenizer(str);

    System.out.println("---- Split by space ------");
    while (st.hasMoreElements()) {
                String s = System.out.println(st.nextElement());

                if (StringUtils.isNumeric(s) {
                    number = s;
                    continue;  
            }   

                if(s.indexOf("Apt")) {
                   aptNumber = s.substring(s.indexOf("Apt"),s.length-1);
                   continue;
                }

    }

    System.out.println("---- Split by comma ',' ------");
    StringTokenizer st2 = new StringTokenizer(str, ",");

    while (st2.hasMoreElements()) {
        System.out.println(st2.nextElement());
    }
}

答案 1 :(得分:1)

如果您利用免费提供的美国邮政服务邮政编码查找器(https://tools.usps.com/go/ZipLookupAction!input.action),您可以获得标准格式的地址。 USPS记录了该格式的有效选项,并且可以更容易地编写非常复杂的正则表达式或许多简单的正则表达式来阅读标准表格。

答案 2 :(得分:1)

我仍在努力,但对于将来可能需要这样做的任何人:

import java.util.Arrays;
import java.util.StringTokenizer;
import org.apache.commons.lang3.*;

class AddressTest{
public static void main (String[] arguments){
   String adr1 = "100 village rest court";
   //String adr2 = "1000 Arbor lane Apt. 21-D";
   String reader = new String();
   String holder = new String();
   StringTokenizer a1 = new StringTokenizer(adr1);
   String[] HouseNbr = new String[9];
   String[] StreetName = new String[20];
   String[] Apartment = new String[5];
   int counter = 0;

   while(a1.hasMoreElements()){
       reader = a1.nextElement().toString();
       System.out.println("Reader: " + reader);
       if(StringUtils.isNumeric(reader)){
           String[] HNBR = reader.split("");
           for(int i = 1; i <= reader.length();i++){
               System.out.println("HNBR:_" + HNBR[i]);
               HouseNbr[i-1] = HNBR[i];   
           }
       }
       else if(StringUtils.startsWith(reader, "Apt.")){
           holder = a1.nextElement().toString();
           String[] ANBR = holder.split("");
           for(int j = holder.length(); j >= 0;j--){
               Apartment[j] = ANBR[j];
           }

       }
       else{
           String STR[] = reader.split("");
           for(int k = 1; k <= reader.length();k++){
               if(counter == StreetName.length){
                   break;
               }
               else{
                   StreetName[counter] = STR[k];
                   if(counter < StreetName.length){
                       counter++;
                   }
               }
           }
           if((counter < StreetName.length) && a1.hasMoreElements()){
               StreetName[counter] = " ";
               counter++;
           }
       }

   }
   System.out.println(Arrays.toString(HouseNbr) + " " + Arrays.toString(StreetName)                
       + " " + Arrays.toString(Apartment));
    }   
}
相关问题