 <p>Your code cannot be compiled because in java character <code>'</code> ( single quote ) is used to mark one character. In order to define string you should use double quote <code>"</code>. </p> <p>In your case I believe that you wanted to check whether your string contains digits only and were confused with regular expression syntax you tried to use incorrectly.</p> <p>You can either rewirte your if statement as following:</p> <p>char c = str.charAt(i); <code>if(c&gt;= '0' &amp;&amp; c &lt;= 9) {</code></p> <p>or use pattern matching, e.g. <code>Pattern.compile("\\d+").matcher(str).matches()</code></p> <p>In this case you even do not need to implement any loop.</p>