I've been getting this exception and I've no idea how to go about fixing it:
java.lang.ArrayIndexOutOfBoundsException: 3
in the while
loop. Here's my code:
public class NameSearch {
static String[] names = new String[3];
void populateStringArray() {
names[0] = "Ben";
names[1] = "Thor";
names[2] = "Zoe";
names[3] = "Kate";
}
public static void main(String[] args) {
String pName;
int max = 4;
int current = 1;
boolean found = false;
Scanner scan = new Scanner(System.in);
System.out.println("What player are you looking for?");
pName = scan.next();
while (found == false && current <= max) {
if (names[current] == pName) {
found = true;
} else {
current = current + 1;
}
}
if (found == true) {
System.out.println("Yes, they have a top score");
} else {
System.out.println("No, they do not have a top score");
}
}
}
The code is meant to ask the user to input a name and it will check to see if the name is in the array (in a nutshell).
My IDE (Eclipse) says that the error lies in the line if (names[current] == pName){
.