引起原因:java.util.NoSuchElementException

|| 大家好,我正在使用以下代码 代码可以在下面的字符串中正常工作
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)
    
已邀请:
        使用
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--------------------\");
                  }
    }
        }
    
        我不确定您会发生什么:如果有六个令牌,您只能拨打6次6次。那就是ѭ7的工作原理。您可以使用
countTokens()
查看有多少个令牌,或者可以使用ѭ9check检查下一次调用ѭ6whether是否会引发异常。     
        for循环中的语句执行了7次,但StringTokenizer仅包含6个字符串。尝试将for循环替换为:
int i=0;
while(st.hasMoreTokens()) {
address[i] = st.nextToken(\",\");
System.out.println(\"address \"+i+\": \"+address[i]);   
i++; 
}
    

要回复问题请先登录注册