5

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

7 Answers 7

7

In the second snippet, the if-clause will always be true, because i will always be not 2 or not 5 (because it cannot be 2 and 5 at the same time)

For what you want to do your first snippet is fine, it does exactly what you want: If your are not at the third element and neither at the 6th element, you want to parse, else you are at one of the points in the date.

Sign up to request clarification or add additional context in comments.

1 Comment

After thinking a while about your answer i understood it, thank you very much for the fast answer!
3

Key point is here : In && if first case is false, second case won't check.

true && false // Evaluates false because the second is false 
false && true // Evaluates false because the first is false 
true && true // Evaluates true because both are true 
false && false // Evaluates false because the first is false

and

In || if first case is true , second case won't check.

true || false // Evaluates true because the first is true 
false || true // Evaluates true because the second is true 
true || true // Evaluates true because the first is true
false || false // Evaluates false because both are false

1 Comment

"false && false // Evaluates false because both are false" - is not true. because the first one is false , so the second one is untested
3

Although not necessarily your issue which LionC notes.

the 4 and and or logic operators follow the following logic:

& is as you describe && above.

false + false = false
true  + false = false
false + true  = false
true  + true  = true

| is as you describe || above

false + false = false
true  + false = true
false + true  = true
true  + true  = true

&& and || are shortcut operators that don't test the second value if it would make no difference to the result.

&&

false + untested = false
true  + false = false
false + untested = false
true  + true  = true

||

false + false = false
true  + untested = true
false + true  = true
true  + untested  = true

Comments

1

Your second snippet is not working because it should be

if(i != 2 && i != 5)

and your first snippet is not throwing error because of same reason that you have used && there.

1 Comment

You told me everything i knew before.. but thanks anyway
1

Pick a number, any number. It is either not equal 2, or not equal 5.

Therefore the expression (i != 2 || i != 5) can be statically (by the compiler) transformed into just true and the whole if control structure eliminated.

The obvious result is a NumberFormatException if any of the characters is not a digit.

Comments

1

I'm not giving answer for your question but suggesting better code.

public class splitToArray {
    public static void main(String[] args) {
        String input = "30.09.1993";    
        input = input.replace(".", " ");
        String array[] = input.split(" ");
        for(String value : array){
            System.out.println(value);
        }
    }
}

Output:
30
09
1993

Now use Integer.parseInt to get integer value. This will also work when you have String input = "30.9.1993"; [0 removed from 09]

1 Comment

Thanks for your advice but i need to create a digit sum from the birthdate, thats why i need every single number.
1

i can only have one value at a time.
So any of the following conditions are valid:

if(i != 2 && i != 5) //means `i` is not any 2 AND i is not 5

or

if(i == 2 || i == 5) //means `i` is either 2 OR 5

Note: (i != 2 || i != 5) is always true because at any time

  1. If i = 2, LHS i is not equal to 2 is FALSE and RHS i is not equal to 5 is TRUE
  2. If i = 5, LHS i is not equal to 2 is TRUE and RHS i is not equal to 5 is FALSE
  3. For all other value of i: Both LHS and RHS are true.

3 Comments

"|" does not stand for OR :)
@rookieB it does stands for OR, but bitwise OR, not for logical OR ;) thanks for noticing :)
well i don't see you are using bitwise OR as i thought it was a typo

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.