大家好我與下面的代碼 代碼工作是做工精細下面的字符串 StringTokenizer st = new StringTokenizer("abc,koch, Ranchi, zalkhand, NY, 10001, India");
產生的原因:java.util.NoSuchElementException
但是當我像=「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)
非常感謝您......代碼的工作... –