Hello dear Programmers,
I have a String String input = "30.09.1993";
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.
I fixed my problem and everything works fine but I'm confused with the logic of the &&
and ||
operators.
This is my working code:
String input = "30.09.1993";
int[] eachNumbers = new int[8];
int x = 0;
for(int i = 0; i <= 9; i++){
if(i != 2 && i != 5){
eachNumbers[x] = Integer.parseInt(input.substring(i, i+1));
x++;
}
}
And this is the code which doesnt work:
String input = "30.09.1993";
int[] eachNumbers = new int[8];
int x = 0;
for(int i = 0; i <= 9; i++){
if(i != 2 || i != 5){
eachNumbers[x] = Integer.parseInt(input.substring(i, i+1));
x++;
}
}
The only difference between these two code snippets are the operators in the if-clause.
I thought that the results for these operators are:
&&
operator:
false + false = false
true + false = false
false + true = false
true + true = true
||
operator:
false + false = false
true + false = true
false + true = true
true + true = true
So in my opinion the second code snippet should work and the first should throw a NumberFormatException
. But thats not the case.
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.
Greetings Lukas Warsitz