1

Hello all i am working with following code code is work fine with below string StringTokenizer st = new StringTokenizer("abc,koch, Ranchi, zalkhand, NY, 10001, India");

but when i remove any element in string like = "abc" it show

 Caused by: java.util.NoSuchElementException" exception..
 StringTokenizer st = new StringTokenizer("koch, Ranchi, zalkhand, NY, 10001, India"); 

Code:

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(""));
        //}  


    }
    }

This is ERROR :-

> 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)
Android
  • 8,995
  • 9
  • 67
  • 108
Prashant Kadam
  • 551
  • 1
  • 12
  • 25

3 Answers3

3

I'm not sure what you're expecting to happen: if there are six tokens, you can only call nextToken() six times. That's just how StringTokenizer works. You can use countTokens() to see how many tokens there are, or you can use hasMoreTokens() to check whether the next call to nextToken() will throw an exception.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
2

The statement within the for loop is being carried out 7 times but the StringTokenizer contains only 6 strings. Try replacing the for loop with:

int i=0;
while(st.hasMoreTokens()) {
address[i] = st.nextToken(",");
System.out.println("address "+i+": "+address[i]);   
i++; 
}
dragonfly
  • 411
  • 3
  • 11
2

use StringTokenizer st = new StringTokenizer("koch, Ranchi, zalkhand, NY, 10001, India", ","); to create string tokenizer. I have modified your code now its working fine.

Use the following code

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--------------------");
                  }
    }
        }
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243