Lukas Warsitz
Lukas Warsitz

Reputation: 1239

Logic of Java Operators && and ||

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

Upvotes: 5

Views: 332

Answers (7)

Caleryn
Caleryn

Reputation: 1084

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

Upvotes: 3

Jatin Malwal
Jatin Malwal

Reputation: 5263

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

Upvotes: 3

Infinite Recursion
Infinite Recursion

Reputation: 6557

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.

Upvotes: 1

Vicky Thakor
Vicky Thakor

Reputation: 3916

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]

Upvotes: 1

Marko Topolnik
Marko Topolnik

Reputation: 200158

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.

Upvotes: 1

A Paul
A Paul

Reputation: 8251

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.

Upvotes: 1

LionC
LionC

Reputation: 3106

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.

Upvotes: 7

Related Questions