Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're incrementing <code>i</code> in the each of the blocks <em>as well</em> as in the main loop "header". Indeed, because you've got one <code>i++;</code> in an <code>else</code> statement for the last <code>if</code> statement, you're sometimes incrementing <code>i</code> twice during the loop.</p> <p>Just get rid of all the <code>i++;</code> statements other than the one in the <code>for</code> statement declaration. For example:</p> <pre><code>newstring=""; for(int i=0;i&lt;dataIn.length();i++) { temp=(int)dataIn.charAt(i); if(temp&gt;46&amp;&amp;temp&lt;58) { newstring=newstring+dataIn.charAt(i); } if(temp&gt;96&amp;&amp;temp&lt;123) { newstring=newstring+dataIn.charAt(i); } if(temp&gt;64&amp;&amp;temp&lt;91) { newstring=newstring+Character.toLowerCase(dataIn.charAt(i)); } } </code></pre> <p>I wouldn't stop editing there though. I'd also:</p> <ul> <li>Use a <code>char</code> instead of an <code>int</code> as the local variable for the current character you're looking at</li> <li>Use character literals for comparisons, to make it much clearer what's going on</li> <li>Use a <code>StringBuilder</code> to build up the string</li> <li>Declare the variable for the output string for the current line within the loop</li> <li>Use <code>if</code> / <code>else if</code> to make it clear you're only expecting to go into <em>one</em> branch</li> <li>Combine the two paths that both append the character as-is</li> <li>Fix the condition for numbers (it's incorrect at the moment)</li> <li>Use more whitespace for clarity</li> <li>Specify a locale in <code>toLower</code> to avoid "the Turkey problem" with I</li> </ul> <p>So:</p> <pre><code>String line; while((line = file.readLine()) != null) { StringBuilder builder = new StringBuilder(line.length()); for (int i = 0; i &lt; line.length(); i++) { char current = line.charAt(i); // Are you sure you want to trim 0? if ((current &gt;= '1' &amp;&amp; current &lt;= '9') || (current &gt;= 'a' &amp;&amp; current &lt;= 'z')) { builder.append(current); } else if (current &gt;= 'A' &amp;&amp; current &lt;= 'Z') { builder.append(Character.toLowerCase(current, Locale.US)); } } System.out.println(builder); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload