Q:

Unable to get the correct output in the java pattern matching

/*Match pattern ab in the pattern ababbaba and print the number of times ab is in the pattern , and also print the index value of a in ab*/
package stringmatch;

public class StringMatch {

    public static void main(String[] args) {

        String name="ababbaba";
        int len,index=0,count=0,temp=0,in,i=index+1;
        String m;
        char ch,c;
       // String match="ab";
        len=name.length();
        for(index=0;index<=len-1;index++)
        {       
            ch=name.charAt(index);
            if(name.charAt(index)=='a'&&name.charAt(i+1)=='b')
            {
                count++;
                temp=index;
                System.out.println("ab is "+count+" "+"index is "+temp);
                //i++;
            }

       }       
    }        //System.out.println(count+" ");    
}

A:

You could rewrite your code as follows,
String name="ababbaba";
    int len, count=0;

    // String match="ab";
    len=name.length();
    for(int index=0; index<=len-2;index++)
    {
        if(name.charAt(index)=='a' && name.charAt(index+1)=='b')
        {
            count++;
            System.out.println("ab is "+count+" "+"index is "+ index);
        }

    }

