当检测到“detele”关键字时,程序设计在arraylist中搜索匹配的名称,并返回一个索引值表示arraylist中的索引,然后将记录删除。现在在数组列表中,一条记录的名称为“Testing One”,但程序找不到该名称。我测试了一下,程序返回-1表示没有这样的记录。但是当我使用 println(personList.get(0).getName()) 时,输出显示有一个像“Testing One”这样的空格。
输出是:
-1
《试一试》
名称不存在,删除失败!
else if(words[0].equalsIgnoreCase("delete"))
{
if(words.length<2)
{
System.out.println("Please enter the name of record you want to delete");
}
else
{
String name = "";
if(Functions.nameValidation(words[1]))
{
for(int i = 2; i < words.length; i++)
{
name = words[1] + " " + words[i];
}
if(Functions.nameValidation(name))
{
int index = Functions.searchPeopleByName(personList, name);
System.out.println(index);
System.out.println(personList.get(0).getName())
if(index>=0)
{
personList.remove(index);
FileIO.outData(personList, outputFileName);
}
else
{
System.out.println("The name does not exist, please check again");
}
}
else
{
System.out.println("The name is invalid, please check again!!!");
}
}
else
{
System.out.println("The name is invalid, please check again");
}
}
}
public static boolean nameValidation(String name)
{
for (int i = 0; i < name.length(); i++) {
if ((!Character.isLetter(name.charAt(i))) && (name.codePointAt(i) != 32))/*space*/ {
return false;
}
}
return true;
}
public static int searchPeopleByName(ArrayList<Person> personList, String name)
{
for(int i=0; i<personList.size();i++)
{
if(personList.get(i).getName().equalsIgnoreCase(name))
return i;
}
return -1;
}