我正在尝试编写一种搜索名称数组的方法;能够通过输入“*”作为输入字符串中的第一个字符或单独输入来退出搜索......逻辑似乎正在逃避我。我不确定我做错了什么。如果找到,则该方法应返回数组元素编号,否则返回-1。
public int searchNames(String [] names) throws IOException{
Scanner keyboard = new Scanner(System.in);
String line = "";
while (line.charAt(0) != '*'){
System.out.print("Enter a name to be searched, and an '*' to exit: ");
line = keyboard.nextLine();
for (int i = 0; i<name.length; i++){
if (names[i].compareTo(line) == 0){return i;}
}//end for loop
}//end while loop
return -1;
}//end searchNames