我想分开街道,城市,州,国家,邮政编码
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):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 )
像这样
那么如何分隔上面的字符串?
答案 0 :(得分:1)
令牌和分裂是必要的,但还不够。
你的问题是,如果你遇到一个多令牌项目,你和你的程序都需要足够的上下文来理解一个令牌结束和下一个令牌开始的位置。
这与美国的问题相同。鉴于此地址:
100 Main Street San Francisco CA 09654
你的大脑有足够的背景知道“旧金山”是城市,州代码是“CA”,但你如何告诉计算机程序如何做?
您需要一个更了解地址的解析器。如果分隔符描绘了每个项目的开始和结束位置,那么这将有所帮助,因此多标记项目不会成为问题。
您可以使用明确的令牌分隔符来解决它:
100 Main Street~San Francisco~CA~09654
在“〜”分开,一切都很好。
答案 1 :(得分:1)
你可以创建一个数组并在解析地址后使用StringTokenizer.But将令牌放入该数组中,你将无法区分哪个是城市或邮政编码。 这是非常困难的,所以尝试使XML格式化。如果City缺席,则设置默认值,以便您可以使用缺席字段。
答案 2 :(得分:0)
您应该创建一个应该接受五个输入的解析器。如果缺少某些数据,则替换为null
1)“Kanaka,Ranchi,zalkhand,10001,India”
2)null,“Ranchi,zalkhand,10001,India”//如果街道缺失
3)“Kanaka,null,zalkhand,10001,India”//如果缺少城市
将此类值传递给StringTokenizer。
答案 3 :(得分:0)
正如所指出的,你需要一种正确的解析XML的方法,其他一切都很简单。
答案 4 :(得分: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--------------------");
}
}
}