大家好,我正在使用以下代码
代码在下面的字符串工作正常
StringTokenizer st = new StringTokenizer("abc,koch, Ranchi, zalkhand, NY, 10001, India");
但当我删除字符串中的任何元素时= =“abc” 它显示
Caused by: java.util.NoSuchElementException" exception..
StringTokenizer st = new StringTokenizer("koch, Ranchi, zalkhand, NY, 10001, India");
代码:
package com.test;
import 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");
System.out.println("Tokens are seperated");
int i=0;
String address[] = new String[7];
String contlocation="";
String contfirst_street="";
String contsecond_street="";
String contcity="";
String contstate="";
String contzipcode="";
String contcountry="";
/* while(st.hasMoreTokens())
{
*/
for(i=0;i<7;i++)
{
address[i] = st.nextToken(",");
System.out.println("address "+i+": "+address[i]);
}
if(address.length==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(address.length==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(address.length==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(address.length==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(address.length==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--------------------");
}
// System.out.println(st.nextToken(""));
//}
}
}
这是错误: -
> 06-23 11:12:59.931:
> ERROR/AndroidRuntime(794): FATAL
> EXCEPTION: main 06-23 11:12:59.931:
> ERROR/AndroidRuntime(794):
> java.lang.RuntimeException: Unable to
> start activity
> ComponentInfo{com.test/com.test.Test}:
> java.util.NoSuchElementException 06-23
> 11:12:59.931:
> ERROR/AndroidRuntime(794): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
> 06-23 11:12:59.931:
> ERROR/AndroidRuntime(794): at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
> 06-23 11:12:59.931:
> ERROR/AndroidRuntime(794): at
> android.app.ActivityThread.access$2300(ActivityThread.java:125)
> 06-23 11:12:59.931:
> ERROR/AndroidRuntime(794): at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
> 06-23 11:12:59.931:
> ERROR/AndroidRuntime(794): at
> android.os.Handler.dispatchMessage(Handler.java:99)
> 06-23 11:12:59.931:
> ERROR/AndroidRuntime(794): at
> android.os.Looper.loop(Looper.java:123)
> 06-23 11:12:59.931:
> ERROR/AndroidRuntime(794): at
> android.app.ActivityThread.main(ActivityThread.java:4627)
> 06-23 11:12:59.931:
> ERROR/AndroidRuntime(794): at
> java.lang.reflect.Method.invokeNative(Native
> Method) 06-23 11:12:59.931:
> ERROR/AndroidRuntime(794): at
> java.lang.reflect.Method.invoke(Method.java:521)
> 06-23 11:12:59.931:
> ERROR/AndroidRuntime(794): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
> 06-23 11:12:59.931:
> ERROR/AndroidRuntime(794): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
> 06-23 11:12:59.931:
> ERROR/AndroidRuntime(794): at
> dalvik.system.NativeStart.main(Native
> Method) 06-23 11:12:59.931:
> ERROR/AndroidRuntime(794): Caused by:
> java.util.NoSuchElementException 06-23
> 11:12:59.931:
> ERROR/AndroidRuntime(794): at
> java.util.StringTokenizer.nextToken(StringTokenizer.java:272)
> 06-23 11:12:59.931:
> ERROR/AndroidRuntime(794): at
> java.util.StringTokenizer.nextToken(StringTokenizer.java:287)
> 06-23 11:12:59.931:
> ERROR/AndroidRuntime(794): at
> com.test.Test.onCreate(Test.java:34)
> 06-23 11:12:59.931:
> ERROR/AndroidRuntime(794): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
> 06-23 11:12:59.931:
> ERROR/AndroidRuntime(794): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
答案 0 :(得分:3)
我不确定您期望发生什么:如果有六个令牌,您只能拨打nextToken()
六次。这就是StringTokenizer
的工作原理。您可以使用countTokens()
查看有多少令牌,也可以使用hasMoreTokens()
检查下一次调用nextToken()
是否会引发异常。
答案 1 :(得分:2)
for循环中的语句正在执行7次,但StringTokenizer只包含6个字符串。尝试用以下代码替换for循环:
int i=0;
while(st.hasMoreTokens()) {
address[i] = st.nextToken(",");
System.out.println("address "+i+": "+address[i]);
i++;
}
答案 2 :(得分:2)
使用StringTokenizer st = new StringTokenizer("koch, Ranchi, zalkhand, NY, 10001, India", ",");
创建字符串标记生成器。
我现在修改了你的代码它的工作正常。
使用以下代码
import 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--------------------");
}
}
}