I want to separate street, city, state, country, zip code
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++;
}
}
}
}
output is :
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
Above code is work fine with String “Kanaka, Ranchi, zalkhand, 10001, India”
My problem is I parse address string from xml which is not good format
ex. 1) "Kanaka, Ranchi, zalkhand, 10001, India"
2) "Ranchi, zalkhand, 10001, India" ---> kanaka(street is absent )
output :
06-23 09:23:37.070: INFO/System.out(435): street = 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): country=
3) "zalkhand, 10001, India"
4) Kanaka zalkhand, 10001, India" (, is missing )
like this
so how to separate above string?
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.