1

I've this string

227305593149516.jpg;221563849532915.3gp;

I split each file name with ; charecter as you can see .I use this code inside my activity to make them apart :

mystring="227305593149516.jpg;221563849532915.3gp;";
StringTokenizer tokenizer = new StringTokenizer(mystring, ";");
                while (tokenizer.hasMoreTokens()) {
                    String token = tokenizer.nextToken();
                    if(!token.contains(".3gp")){//aks bud
                        imges[cimage]=token;
                        cimage++;
                    }else{//film bud
                        if(cvide==1)
                            video1=token;
                        else
                            video2=token;
                        cvide++;
                    }
                }

It's very strange ,I run this code on emulator and 2 other devices and it works find but I run the same app on another phone and it crashed , this is the log :

java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at com.arousshow.persiandesigners.AddDetails$GetContacts.onPostExecute(AddDetails.java:160)
at com.arousshow.persiandesigners.AddDetails$GetContacts.onPostExecute(AddDetails.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)

this is the line that make the app crashes (AddDetails.java:160) :

if(!token.contains(".3gp")){

the model phone is PHONE_MODEL = GT-I9300 and ANDROID_VERSION = 4.1.2 .

Could you help me ? Thanks

2
  • i don't think this line could cause such error, are you sure the code you are showing is the one running on devices? yo could changed some lines, so the code line are not sync. another question, what is the declaration of imges? because i think imges[cimage]=token; is the line makes the error Commented Feb 17, 2015 at 16:05
  • @ChrisCromer may be, because it looks the array imges has length=1 and the condition contains(".3gp") applied on 2 entries so OP gots the error, though the current string does not show that 2 items matches the condition! Commented Feb 17, 2015 at 16:07

1 Answer 1

1

I don't have an android SDK set up at the moment, but in Java 8, this works for me:

public static void main(String[] args) {
    String mystring = "227305593149516.jpg;221563849532915.3gp;";
    StringTokenizer tokenizer = new StringTokenizer(mystring, ";");
    while (tokenizer.hasMoreTokens()) {
        String token = tokenizer.nextToken();
        if (!token.contains(".3gp")) {
            System.out.println(token + " has 3gp");
        } else {
            System.out.println(token + " does not have 3gp");
        }
    }
}

"227305593149516.jpg has 3gp"
"221563849532915.3gp does not have 3gp"

I suspect that Android may be adding an additional token after the last ';', which consists of an empty string. Does adding an empty string check like so take care of the indexArray problem?

if (!token.isEmpty() && !token.contains(".3gp")) {

According to android documentation, you should be using split instead anyway. StringTokenizer: "Breaks a string into tokens; new code should probably use split(String)."

Another side note, you could more correctly check if the token 'endsWith' the suffix you care about. String::EndsWith

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.