check if string formed using stated set of rules or not
<p>Check if string formed using stated set of rules or not. Generated using the following rules:</p>
<blockquote>
<p><strong>a.</strong> the string begins with an 'a'</p>
<p><strong>b.</strong> each 'a' is followed by nothing or an 'a' or &quot;bb&quot;</p>
<p><strong>c.</strong> each &quot;bb&quot; is followed by nothing or an 'a'</p>
</blockquote>
<p>I tried the following code:</p>
<pre><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);
}
</code></pre>
<p>is the code all right...???
For input = aab the output should be false and for input =abba the output should be true.</p>