import java.util.*;
public class Lnrsrc {
public static int linearsearch(int number[],int key)
{
for(int i=0;i<=number.length;i++)
{
if(number[i]==key)
{
return i;
}
}
return -1;
}
public static void main(String args[])
{
int number[]={2,4,6,8,10,12,14,16};
int key=54;
int index=linearsearch( number, key);
if(index==-1)
{
System.out.println("NOT found");
}
else
{
System.out.println(index);
}
}
}
//this is my code
and the problem is My key is not in the array ,so it supposed to return me “Not found” .Instead it’s giving me this “Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: Index 8 out of bounds for length 8″
//
i was expecting it would give me NOT found
New contributor
Nur Ahammed Tonmoy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.