0

我想分开街道、城市、州、国家、邮政编码

String = Kanaka, Ranchi, zalkhand, 10001, India

public class Test extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        StringTokenizer st = new StringTokenizer(" Kanaka, Ranchi, zalkhand, 10001, India"); 
        System.out.println("Tokens are seperated");
        int i=0;
        String street,city,state,zipcode,country;
        while(st.hasMoreTokens())
        {

                if(i==0)
                {
                        street = st.nextToken(",");
                   System.out.println("street ="+street);
                   i++;
                }
                else if(i==1)
                {
                       city = st.nextToken(",");
                        System.out.println("city= "+city);
                        i++;
                }
                else if(i==2)
                {
                       state = st.nextToken(",");
                        System.out.println("state ="+state);
                        i++;
                }
                else if(i==3)
                {
                        zipcode = st.nextToken(",");
                        System.out.println("zipcode= "+zipcode);
                            i++;
                }
                else if(i==4)
                {
                        contry = st.nextToken(",");
                        System.out.println("country= "+country);
                        i++;
                }

        }

    }
}

输出是:

06-23 09:23:37.070: INFO/System.out(435): street = Kanaka
06-23 09:23:37.080: INFO/System.out(435): city=  Ranchi
06-23 09:23:37.080: INFO/System.out(435): state = zalkhand
06-23 09:23:37.080: INFO/System.out(435): zipcode=  10001
06-23 09:23:37.080: INFO/System.out(435): country=  India

上面的代码适用于 String "Kanaka, Ranchi, zalkhand, 10001, India"

我的问题是我从 xml 解析地址字符串,格式不好

ex. 1) "Kanaka, Ranchi, zalkhand, 10001, India"

    2) "Ranchi, zalkhand, 10001, India"   ---> kanaka(street is absent )

输出:06-23 09:23:37.070:INFO/System.out(435):街道 = Ranchi 06-23 09:23:37.080:INFO/System.out(435):city=zalkhand 06-23 09:23 :37.080: INFO/System.out(435): state = 10001 06-23 09:23:37.080: INFO/System.out(435): zipcode= India 06-23 09:23:37.080: INFO/System.out (435): 国家=

    3) "zalkhand, 10001, India"
    4) Kanaka zalkhand, 10001, India" (, is missing )

像这样

那么如何分离上面的字符串?

4

5 回答 5

1

令牌和拆分是必要的,但还不够。

Your problem is that if you encounter a multi-token item you and your program both need enough context to understand where one token ends and the next begins.

It's the same problem here in the US. Given this address:

100 Main Street San Francisco CA 09654

Your brain has enough context to know that "San Francisco" is the city, and the state code is "CA", but how do you tell a computer program how to do it?

You need a parser that has more knowledge of what an address is. It would help if the separators delineated where each items started and ended, so multi-token items would not be a problem.

You can solve it with a clear token delimiter:

100 Main Street~San Francisco~CA~09654

Split at the "~" and all is well.

于 2011-06-23T09:44:45.777 回答
1

You can Create a array and after parsing address String put the tokens in that array using StringTokenizer.But via this you won't be able to differentiate which is City or ZIP code. It is quite difficult so try to make XML well formated.Like if City is absent put a default value so you can use as absent field.

于 2011-06-23T09:46:17.547 回答
0

You should create a parser which should take five inputs. and replace null if some data is missing

1) "Kanaka, Ranchi, zalkhand, 10001, India"

2) null, "Ranchi, zalkhand, 10001, India" // if street is missing

3) "Kanaka, null, zalkhand, 10001, India" // if city is missing

pass this type of value to your StringTokenizer.

于 2011-06-23T10:02:07.717 回答
0

As pointed out you need a correct way of parsing the XML and all else would be easy.

于 2011-06-23T10:38:21.153 回答
0

mport java.util.StringTokenizer;

import android.app.Activity; import android.os.Bundle;

public class Test extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


StringTokenizer st = new StringTokenizer("koch, Ranchi, zalkhand, NY, 10001, India", ","); 
        int numberOfToken = st.countTokens();
        System.out.println("Tokens are seperated");
        int i=0;
        String address[] = new String[numberOfToken];

         String contlocation="";
         String contfirst_street="";
         String contsecond_street="";
         String contcity="";
         String contstate="";
         String contzipcode="";
         String contcountry="";


      /*  while(st.hasMoreTokens())
        {

          */      

              for(i=0;i<numberOfToken;i++)
              {
                    address[i] = st.nextToken();
                   System.out.println("address "+i+": "+address[i]);
              }

              if(numberOfToken==7)
              {

                  System.out.println("\n-------------address.length==7--------------------");

                  contlocation=address[0];
                  System.out.println("contlocation :"+contlocation);

                  contfirst_street=address[1];
                  System.out.println("contfirst_street :"+contfirst_street);

                  contsecond_street=address[2];
                  System.out.println("contsecond_street :"+contsecond_street);

                  contcity=address[3];
                  System.out.println("contcity :"+contcity);

                  contstate=address[4];
                  System.out.println("contstate :"+contstate);

                  contzipcode=address[5];
                  System.out.println("contzipcode :"+contzipcode);

                  contcountry=address[6];
                  System.out.println("contcountry :"+contcountry);

                  System.out.println("\n-------------address.length==7--------------------");
              }
              else if(numberOfToken==6)
              {
                  System.out.println("\n-------------address.length==6--------------------");

                  contlocation="";
                  System.out.println("contlocation :"+contlocation);

                  contfirst_street=address[0];
                  System.out.println("contfirst_street :"+contfirst_street);

                  contsecond_street=address[1];
                  System.out.println("contsecond_street :"+contsecond_street);

                  contcity=address[2];
                  System.out.println("contcity :"+contcity);

                  contstate=address[3];
                  System.out.println("contstate :"+contstate);

                  contzipcode=address[4];
                  System.out.println("contzipcode :"+contzipcode);

                  contcountry=address[5];
                  System.out.println("contcountry :"+contcountry);

                  System.out.println("\n-------------address.length==6--------------------");
              }
              else if(numberOfToken==5)
              {
                  System.out.println("\n-------------address.length==5--------------------");

                  contlocation="";
                  System.out.println("contlocation :"+contlocation);

                  contfirst_street=address[0];
                  System.out.println("contfirst_street :"+contfirst_street);

                  contsecond_street="";
                  System.out.println("contsecond_street :"+contsecond_street);

                  contcity=address[1];
                  System.out.println("contcity :"+contcity);

                  contstate=address[2];
                  System.out.println("contstate :"+contstate);

                  contzipcode=address[3];
                  System.out.println("contzipcode :"+contzipcode);

                  contcountry=address[4];
                  System.out.println("contcountry :"+contcountry);

                  System.out.println("\n-------------address.length==5--------------------");
              }

              else if(numberOfToken==4)
              {
                  System.out.println("\n-------------address.length==4--------------------");

                  contlocation="";
                  System.out.println("contlocation :"+contlocation);

                  contfirst_street=address[0];
                  System.out.println("contfirst_street :"+contfirst_street);

                  contsecond_street="";
                  System.out.println("contsecond_street :"+contsecond_street);

                  contcity=address[1];
                  System.out.println("contcity :"+contcity);

                  contstate=address[2];
                  System.out.println("contstate :"+contstate);

                  contzipcode="";
                  System.out.println("contzipcode :"+contzipcode);

                  contcountry=address[3];
                  System.out.println("contcountry :"+contcountry);

                  System.out.println("\n-------------address.length==4--------------------");
              }

              else if(numberOfToken==3)
              {
                  System.out.println("\n-------------address.length==3--------------------");

                  contlocation="";
                  System.out.println("contlocation :"+contlocation);

                  contfirst_street=address[0];
                  System.out.println("contfirst_street :"+contfirst_street);

                  contsecond_street="";
                  System.out.println("contsecond_street :"+contsecond_street);

                  contcity=address[1];
                  System.out.println("contcity :"+contcity);

                  contstate="";
                  System.out.println("contstate :"+contstate);

                  contzipcode="";
                  System.out.println("contzipcode :"+contzipcode);

                  contcountry=address[2];
                  System.out.println("contcountry :"+contcountry);

                  System.out.println("\n-------------address.length==3--------------------");
              }
}
    }
于 2011-06-23T13:28:01.527 回答