Java Comparator compareToIgnoreCase

Java Comparator compareToIgnoreCase,java,sorting,arraylist,nullpointerexception,comparator,Java,Sorting,Arraylist,Nullpointerexception,Comparator,我正在尝试使用compareToIgnoreCase对字符串数组进行排序。 该字符串包含以下名称: 比利·琼 当我尝试比较它们时,我得到一个空指针异常。我想这是因为名字和姓氏之间的空格。如何比较全名 谢谢 class CompareStudentNames implements Comparator{ //Sorts the students by name ignoring case. @Override public int compare(Object o1, Object o2)

我正在尝试使用compareToIgnoreCase对字符串数组进行排序。
该字符串包含以下名称: 比利·琼
当我尝试比较它们时,我得到一个空指针异常。我想这是因为名字和姓氏之间的空格。如何比较全名

谢谢

 class CompareStudentNames implements Comparator{

 //Sorts the students by name ignoring case.
@Override
public int compare(Object o1, Object o2) {
    String name1 = ((Student)o1).getName();
    String name2 = ((Student)o2).getName();

return (name1.compareToIgnoreCase(name2));

}
}

编辑---添加到使用比较学生姓名的代码中

private static void newInputFileReading(File f) throws FileNotFoundException{
    String inputLine = null;
    String [] inputSplit = new String[7];
    Boolean proceed = false;
    Scanner enterReader = new Scanner(System.in);
    String name;
        while(!proceed){            
            int stunum = -1;
            try {                   

                Scanner inputReader = new Scanner(f);

                while(inputReader.hasNextLine()){


                    studentNM.add(new Student());
                    ++stunum;
                    inputLine = inputReader.nextLine();
                    inputSplit = inputLine.split(",");
                    testForWord(inputSplit);
                    System.out.println(inputSplit[0]);
                    name = inputSplit[0];
                    System.out.println(name);
                    for(int i = 1; i<8; i++){
                        if(i == 0){
                            studentNM.get(stunum).setName(name);
                        }// where to send the name on run 0                         
                        else if(i <= 4 && i>0){
                            studentNM.get(stunum).setQuiz(testForDouble(inputSplit,i), i-1);
                        }// where to output to runs 1-4
                        else if(i>4 && i <= 6){
                            studentNM.get(stunum).setMids(testForDouble(inputSplit,i),i-5);
                        }// where to output on runs 5 & 6
                        else if( i> 6){
                            studentNM.get(stunum).setFinal(testForDouble(inputSplit,i));
                        }// where to output on the 7th run                          
                    }// for loop to  assign the inputs                      
                }// while scanner has next
                proceed = true;             
                Collections.sort(studentNM, new CompareStudentNames());


            }//try to initalize a new scanner and get & assign the inputs

            catch (FileNotFoundException e) {
                proceed = false;
                studentNM.clear();
                System.out.println("The file appears to have gone missing, please restart the program");
                System.out.println("Press Enter to continue");
                enterReader.nextLine();
                System.out.println("");
            }// catch a file not found exception
            catch(formatError | NumberFormatException e){
                proceed = false;
                studentNM.clear();
                System.out.printf("You input file is formatted incorrectly\nEvery line must start with a word,followed by 7 numbers, seperated by commas.\nPlease reformat your file and try again.\n");
                System.out.println("Press Enter to continue");
                enterReader.nextLine();
                System.out.println("");
            }// catch format error
            catch (Exception e) {
                proceed = false;
                studentNM.clear();
                System.out.println("An unknown error occured, please restart the program");
                e.printStackTrace();
                System.out.println("Press Enter to continue");
                enterReader.nextLine();
                System.out.println("");
            }

        }// while to make sure the first token is a word

}// newInputFileReading

显示空值

你应该键入你与学生的比较词,即
Comparator

类CompareStudentNames实现Comparator{
//按姓名和大小写对学生进行排序。
@凌驾
公共整数比较(学生o1,学生o2){
字符串名称1=o1.getName();
字符串名称2=o2.getName();
返回name1.compareTignoreCase(name2);
}
此外,您还应检查:

  • 您没有对空值进行排序-尤其是一个问题 对于(未初始化)数组/数组元素
  • 所有学生都有(非空)姓名

只需使用String自己的不区分大小写的比较器:
String.不区分大小写\u顺序

e、 g

但只有在比较一个列表或字符串数组时,也就是说,
List

class StudentComparator实现Comparator{
@凌驾
公共整数比较(学生s1、学生s2){
//首先检查空值
返回字符串。不区分大小写。\u顺序。比较(s1.getName(),s2.getName());
}
}

您必须粘贴使用
CompareStudentNames
的代码,这样我们就可以看到如何创建和填写
Student
对象。您将从
i=1
开始for循环,但如果(i==0)则设置名称
if
。谢谢Jason,非常感谢!你的
NullPointerException
与空格无关。你要么比较空字符串,要么在其他地方出错。@jahroy,你是对的,请看Jason的评论你是对的,我排序空值是因为我从1而不是0开始计算。谢谢
System.out.println(studentNM.get(0).getName());
class CompareStudentNames implements Comparator<Student> {

//Sorts the students by name ignoring case.
@Override
public int compare(Student o1, Student o2) {
    String name1 = o1.getName();
    String name2 = o2.getName();

    return name1.compareToIgnoreCase(name2);
}
Collections.sort(myStringList, String.CASE_INSENSITIVE_ORDER);
class StudentComparator implements Comparator<Student> {
   @Override
   public int compare(Student s1, Student s2) {
      // first check for nulls
      return String.CASE_INSENSITIVE_ORDER.compare(s1.getName(), s2.getName());
   }
}