【发布时间】:2015-12-05 17:19:04
【问题描述】:
我遇到了这个异常,但我不知道如何解决它:
java.lang.ArrayIndexOutOfBoundsException: 3
在while 循环中。这是我的代码:
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");
}
}
}
该代码旨在要求用户输入一个名称,它将检查该名称是否在数组中(简而言之)。
我的 IDE (Eclipse) 说错误位于 if (names[current] == pName){ 行中。
【问题讨论】:
标签: java arrays string while-loop