java count substring occurrences in string

Java
 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: