Hi everyone. I wrote a simple code to find if an integer is present in an array, but I get wrong answer. If array is a = {1,2,3,4} and int b = 2, it returns false.
Could you point on my mistake, Here is the code:
public class arrayTester {
/**
* @param args the command line arguments
*/
public static boolean isPresented(int anInt, int[] anArray){
boolean result = true;
for (int i = 0;i<anArray.length;i++){
if (anArray[i]==anInt){
result = true;
}
else{
result = false;
}
}
System.out.println(result);
return result;
}
public static void main(
String[] args) {
int[] a = {2,3,4,5};
isPresented(2,a);
}
}