0

I pass string value to this method.but im getting Array index bound exception please help me.

public void separateCards(String cards)
{
    cardNames+=cards+"-";
    String[] parts = cardNames.split("-");
    String part1 = parts[0]; // 004
    String part2 = parts[1]; // 034556
    Toast.makeText(getActivity(), part1,Toast.LENGTH_SHORT).show();
}
}

My logcat

  03-28 11:27:41.213: E/AndroidRuntime(22392): FATAL EXCEPTION: main
    03-28 11:27:41.213: E/AndroidRuntime(22392): java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
    03-28 11:27:41.213: E/AndroidRuntime(22392):    at com.compareCr.ListvCompare.separateCards(ListvCompare.java:760)
    03-28 11:27:41.213: E/AndroidRuntime(22392):    at com.compareCr.ListvCompare$2$1.onClick(ListvCompare.java:627)
    03-28 11:27:41.213: E/AndroidRuntime(22392):    at android.view.View.performClick(View.java:4261)
    03-28 11:27:41.213: E/AndroidRuntime(22392):    at android.view.View$PerformClick.run(View.java:17356)
    03-28 11:27:41.213: E/AndroidRuntime(22392):    at android.os.Handler.handleCallback(Handler.java:615)
    03-28 11:27:41.213: E/AndroidRuntime(22392):    at android.os.Handler.dispatchMessage(Handler.java:92)
5
  • 1
    what is your String that you are attempting to split over - Commented Mar 28, 2014 at 6:07
  • Is cardNames = 004 and cards = 034556? Commented Mar 28, 2014 at 6:07
  • @Apoorv its cardName just like GoldeCard-platinumcard like that Commented Mar 28, 2014 at 6:08
  • 1
    Use this: cardNames+="-"+cards;. If u understand it, i'll add an answer. Commented Mar 28, 2014 at 6:08
  • it is unclear what does your string contain Commented Mar 28, 2014 at 6:24

5 Answers 5

3

you append "-" at the end of the string and therefore when you split , you get only one value.

therefore this String part2 = parts[1]; throw array index out of bound exception.

you need to put "-" in between string.
Sign up to request clarification or add additional context in comments.

2 Comments

I grant you permission to use the code from my comment: cardNames+="-"+cards; :-)
thank you but i dont think to add that here. because i guess the question man has understand what was the problem.....:p
1

I think there is only single "-" in your cardNames value, that's why you getting error..

So better to use For loop for getting all data from Array

    String[] parts = cardNames.split("-");
    for (int i = 0; i < parts.length; i++) {
        String partsData = parts[i]; 
    }

Updated:

cardNames=cardNames+"-"+cards;

String parts1;
String parts2;
String[] parts = cardNames.split("-");
for (int i = 0; i < parts.length; i++) {
    if(i==0)
        parts1= parts[i];
    else if(i==1)
        parts2= parts[i];
}

2 Comments

i want to divide becouse i want to save in two strings.how do i do that please help me
First you merge two string after then you divide it, why this so ??
1

You add the - in the wrong place. Giving you the value as(I assume the initial value of cardNames is 004)

"004034556-"

You should do:

cardNames += "-" +cards;

Which will give you:

"004-34556"

But seriously what is the purpose of combining the String and split it again at the same way?

Wouldn't it be easy if you just do like this?

String part1 = cardNames; 
String part2 = cards;

Note: You should always check that the index element that you want to access must less that parts.length

1 Comment

Care to explain how this happens?
0

When something with no "-" passed into your method, like "foo" it become "foo-" when u call "foo-".split() it returns ["foo"] therefore if u try to access index 1, it is out of bound.

Comments

-1

Have you print "cards"?

I think cardNames is "somethig-"; then parts = {'something'}

so, parts[1] is out of index.

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.