Q:

check if string formed using stated set of rules or not

Check if string formed using stated set of rules or not. Generated using the following rules:

a. the string begins with an 'a'
b. each 'a' is followed by nothing or an 'a' or "bb"
c. each "bb" is followed by nothing or an 'a'

I tried the following code:
public static void main(String[] args) {
    
    Scanner scn = new Scanner(System.in);
    String str = scn.nextLine();
    boolean b = false;
    if (str.charAt(0) == 'a') {
        if (str.charAt(1) == 'b') {
            if (str.charAt(2) == 'b') {
                b = true;
            } else
                b = false;

        } else
            b = false;
    } else
        b = false;
    System.out.println(b);
}

is the code all right...???
For input = aab the output should be false and for input =abba the output should be true.

A:

If regular expressions are allowed, the pattern (a+(bb)?)+ matches strings that follow the rule (And doesn't match strings that don't).
Otherwise, Your approach can't possibly work without some kind of loop, because the string aaaaaaaaaaa does match the pattern.
Consider the following method, which should handle it.
private static boolean stringMatches(String s) {
  // Handle empty and null cases first.
  if (s == null || s.isEmpty()) return false;

  // So long as the string continues to match the pattern, keep stripping
  // characters from it until it is empty. If you reach empty, it matches the pattern.
  while (! s.isEmpty()) {
    // If the first character isn't 'a', we don't match; return false.
    if (s.charAt(0) != 'a') {
      return false;
    }

    // Check for abb, if so strip all of that, otherwise strip just the a
    if (s.length() >= 3 && "abb".equals(s.substring(0,3)) {
      s = s.substring(3);
    } else {
      s = s.substring(1);
    }
  }
  // Reached empty string, return true.
  return true;
}

