0

I want the sub-array array has been tested in main array. but i get error.

The test I'm doing is right or need to add more conditions?

Finally, I need to print the index of the main array, in which sub-string begins.

Example to answer:

String m="cd e";
String n="abcd efghi";

print>>>> 2(the index in n String)

the error:

 Exception in thread "main" java.lang.StringIndexOutOfBoundsException:         
 String  index out of range: 4
 at java.lang.String.charAt(Unknown Source)
 at test1.main(test1.java:10)

The code:

    String m="cd e";
    String n="abcd efghi";


    for (int i = 0; i < m.length(); i++) {
        for (int j = 0; j < n.length(); j++) {
            if(m.charAt(j)==n.charAt(i) )
                if(m.charAt(j+1)==n.charAt(i+1))
                    System.out.println(i);          
        }   
     }

thank's.

3
  • you dont need this again if(m.charAt(j+1)==n.charAt(i+1)) Commented Jun 6, 2016 at 14:00
  • change 'm.length()' to 'm.length() - 1' in the loops and what do you think j+1 will do if you are testing last item? Commented Jun 6, 2016 at 14:00
  • 1
    Few hints: (1) your i seems to be iterator for m and j for n, so why are you using i for n? (you should name your variables more reasonably, even i1 i2 n1 n2 would cause less confusion). (2) what value will i+1 have when i will be equal to m.length-1? Is there any charAt at position pointed by that value? Commented Jun 6, 2016 at 14:22

3 Answers 3

1

try this: You look through with the n because it has the longest length. I hope this helps. String m="cd e"; String n="abcd efghi";

    for (int i = 0; i < n.length(); i++) {
        for (int j = 0; j <i; j++) {
            if (i <= m.length()){ // to prevent indexoutOfbound exception
                if(m.charAt(j)==n.charAt(i) ) {
                    System.out.println(m.charAt(j)+"-->"+i);
                }
            }
        }
    }

output:

c-->2
d-->3
 -->4
Sign up to request clarification or add additional context in comments.

Comments

0
package default1;

public class Test {
public static void main(String[] args) {
    String m="cd e";
    String n="abcd efghi";

    System.out.println(n.indexOf(m));

}
}

Comments

0
int i = 0;
    int j = 0;
    int index = 0;
    boolean previousMatch = false;
    while (i < n.length()) {
        if (j == m.length()) {
            break;
        }
        if (m.charAt(j) == n.charAt(i)) {
            if (!previousMatch) {
                previousMatch = true;
                index = i;
            }
            j++;
        } else {
            previousMatch = false;
            index = 0;
            j = 0;
        }
        i++;
    }
    if (previousMatch)
        System.out.println(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.