mardi 11 juillet 2017

My if else statement keeps running the else part even though the condition is true for the if part in Java

for (int i = 0; i < arr.length; i++) {
    if (arr[i] == s) {
        System.out.println("The index of " + s + " is " + i);     
    } else {
        System.out.println("The entered element is not in the array");
    }
}

Output:

Enter the Element:
656
The entered element is not in the array
The entered element is not in the array
The index of 656 is 2
The entered element is not in the array
The entered element is not in the array

Aucun commentaire:

Enregistrer un commentaire