Note that there are some explanatory texts on larger screens.

plurals
  1. POLogic of Java Operators && and ||
    primarykey
    data
    text
    <p>Hello dear Programmers, </p> <p>I have a String <code>String input = "30.09.1993";</code> Then i want to save all numbers in this string in an array(Only numbers!). The "." are at index 2 and 5 so i want to skip these parts of my string in my loop with an if-statement.</p> <p>I fixed my problem and everything works fine but I'm confused with the logic of the <code>&amp;&amp;</code> and <code>||</code> operators. </p> <p>This is my working code:</p> <pre><code>String input = "30.09.1993"; int[] eachNumbers = new int[8]; int x = 0; for(int i = 0; i &lt;= 9; i++){ if(i != 2 &amp;&amp; i != 5){ eachNumbers[x] = Integer.parseInt(input.substring(i, i+1)); x++; } } </code></pre> <p>And this is the code which doesnt work:</p> <pre><code>String input = "30.09.1993"; int[] eachNumbers = new int[8]; int x = 0; for(int i = 0; i &lt;= 9; i++){ if(i != 2 || i != 5){ eachNumbers[x] = Integer.parseInt(input.substring(i, i+1)); x++; } } </code></pre> <p>The only difference between these two code snippets are the operators in the if-clause.</p> <p>I thought that the results for these operators are:</p> <p><strong><code>&amp;&amp;</code> operator:</strong> </p> <pre><code>false + false = false true + false = false false + true = false true + true = true </code></pre> <p><strong><code>||</code> operator:</strong></p> <pre><code>false + false = false true + false = true false + true = true true + true = true </code></pre> <p>So in my opinion the second code snippet should work and the first should throw a <code>NumberFormatException</code>. But thats not the case. </p> <p>I'm sure there are some better solutions for what im doing but my question is only about the logic in my if-statement. Can someone explain me the logic behind this? I'm totally confused and thankful for every helping answer.</p> <p>Greetings Lukas Warsitz</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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