2

i'm working on a program for my class but now i have a problem with spliting string and substrings. I have saved data from different classes in a string with a delimiter for later a later split(/). This part works fine and i get all the strings as i should saved in a new String array.

Later i tried to toast them in a for loop (String word: String array) and everything seemed fine. But i have a problem with cutting the substring from word. I would like to get the number (between '-' and 'k') but it always throws a String out of index error and i don't know why. When i tried to toast the position of the strings where i would like to take the substring, it shows them fine but when i try to substring with them it throws an error again.

The Error

"java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.uiuracun/com.example.uiuracun.Bill}: java.lang.StringIndexOutOfBoundsException: length=29; regionStart=22; regionLength=-18"

The Code

package com.example.uiuracun;

import android.R.anim;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Bill extends ListActivity {

    // Removed extra code

    private void split(String[] string) {
        for (String word:string){
            word.trim();
            int start = 0;
            int end = 0;
            start = word.indexOf('-');
            end = word.indexOf('k');
            String c = word.substring(start, end);


            }
        }
    }
4
  • 1
    Could you give an input-output sample? Commented Dec 25, 2013 at 20:06
  • And what is look like your string containing - k you can make a SSCCE you post code that is not necessary, your problem it seems to be in your method split ,much noise there. Commented Dec 25, 2013 at 20:07
  • start should be incremented by one and end should be decremented by one Commented Dec 25, 2013 at 20:10
  • @AniruddhaSarkar, you are half correct. start should be incremented but the endIndex is exclusive (so it is fine). docs.oracle.com/javase/7/docs/api/java/lang/…, int) Commented Dec 25, 2013 at 20:26

2 Answers 2

1

Your error message includes length=29; regionStart=22; regionLength=-18 note the length is negative.

This leads me to believe that the k character appears before the - character. Since your code assume - always comes first, you get an end less than start.

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

Comments

1

Here is some information to help you with that exception.

From the Java docs

IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

UPDATE

I think you should try this:

start = word.indexOf('-');

// tell it to start looking for the 'k' starting from the index
// of the '-' that was found.
end = word.indexOf('k', start);

String c = word.substring(start, end);

1 Comment

Yeah i figured it out... Thanks anyway i really appreciate it

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.