how to count an replace substring string in java

Java
String str = "helloslkhellodjladfjhello";
String findStr = "hello";
int lastIndex = 0;
int count = 0;
while(lastIndex != -1){
    lastIndex = str.indexOf(findStr,lastIndex);
    if(lastIndex != -1){
        count ++;
        lastIndex += findStr.length();
    }
}
System.out.println(count); Scanner scanner = new Scanner(System.in);
        
        String line = scanner.nextLine();
        
        String sub = scanner.nextLine();
        
        int count = 0;
        
        int fromIndex =0;
        
        
        while( (fromIndex = line.indexOf(sub,fromIndex) ) != -1) {
            
            count++;
        
           fromIndex++;
            
        }
       
        System.out.println(count);
Source

Also in Java: