title: Java if-statement being skipped
body: <p>The method below takes in a string and a pattern and returns <em>true</em> if they match each other. A '.' matches 1 char and a '*' matches 0 or more (e.g. <code>expMatch("abc", "a.c")</code> should return <em>true</em>). I added a bunch of print statements to see where I went wrong and it seems like the if statement is being skipped even if the <code>str.length() == 1</code>. </p>
  
  <p>I call it with <code>System.out.println(expMatch("abc", "a*c"));</code></p>
  
  <p>Here is the code:</p>
  
  <pre><code>public static boolean expMatch(String str, String pat)
  {   
      if (str.charAt(0) == pat.charAt(0) || pat.charAt(0) == '.')
      {
          System.out.println("in if");
          System.out.println(str.charAt(0));
          System.out.println(pat.charAt(0));
          System.out.println(str.length());
          if (str.length() == 1)
              return true;
          expMatch(str.substring(1), pat.substring(1));
      }
  
      else if (pat.charAt(0) == '*')
      {   
          System.out.println("in else");
          System.out.println(str.charAt(0));
          System.out.println(pat.charAt(0));
          if (str.length() == 1)
              return true;
          if (str.charAt(0) == pat.charAt(1)) //val of * = 0
              expMatch(str, pat.substring(1));
          else if (str.charAt(1) ==pat.charAt(1))
              expMatch(str.substring(1), pat.substring(1));           
      }
  
      return false;           
  }
  </code></pre>
  
  <p>and the output is:</p>
  
  <pre><code>in if
  a
  a
  3
  in else
  b
  *
  in if
  c
  c
  1
  false
  </code></pre>
  
  <p>Even if the length is 1 it skips the if? Any idea why?
  P.S. I'm not looking for the solution, just why the if statement is being skipped.</p>