'Substring search in String (Java)

I want to search the entered letters in the entered text (Ctrl + f algorithm). I want to keep the indexes of the letters it finds in an array and then print that array to the console. I wrote this code but it is not working properly. Prints the length of the entered text. I'm a beginner and I'm open to critiques to optimize my code :) Could you please help? ///

For example;

My input text : "muammer akca akca"

My input search : "ak"

Output : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 17 out of bounds for length 17 at Assignment5_2.main(Assignment5_2.java:17)

Expected output :

8

13


8 and 13 are "ak" indexes

Another example:

My input text : "Sotirios Delimanolis"

My input search : "ri"

Output : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 17 out of bounds for length 17 at Assignment5_2.main(Assignment5_2.java:17)

Expected output :

4

4 is "ri" index number

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Assignment5_2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String text = scanner.nextLine();
        String search = scanner.nextLine();
        char[] textArray = text.toCharArray();
        char[] searchArray = search.toCharArray();
        int i,j;
        List<Integer> foundedArray = new ArrayList<Integer>();
        boolean foundControl = true;
        for (i=0 ; i< textArray.length ; i++){
            foundControl = true;
        } for (j=0 ; j<searchArray.length ; j++){
            if (textArray[i+j] != searchArray[j]){
                foundControl = false;
                break;
            }
        }
        if (foundControl){
            foundedArray.add(i);
            System.out.println(i);
        }
        for (i=0;i<foundedArray.size() ; i++){
            System.out.println(foundedArray.get(i));
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source