0

compareToIgnoreCase を使用して文字列の配列を並べ替えようとしています。
文字列には次のような名前が含まれています: Billy Jean
それらを比較しようとすると、null ポインター例外が発生します。これは姓と名の間のスペースが原因だと思います。名前全体を比較するにはどうすればよいですか?

ありがとう

 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));

}

}

edit--- Compare Student Names を使用するコードに追加

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

Student オブジェクトが名前を取得しているとは思わない

System.out.println(studentNM.get(0).getName());

null を示します。

4

2 に答える 2

6

Comoarator を Student と入力する必要があります。つまり、次のようになりますComparator<Student>

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);
}

また、次のことを確認する必要があります。

  • nullをソートしていません-特に(初期化されていない)配列/配列要素の問題
  • 生徒はすべて (null 以外の) 名前を持っています
于 2013-09-01T02:30:44.543 に答える
1

String 独自の大文字と小文字を区別しない Comparator を使用するだけです。String.CASE_INSENSITIVE_ORDER

例えば、

Collections.sort(myStringList, String.CASE_INSENSITIVE_ORDER);

ただし、リストまたは文字列の配列を比較している場合のみ。すなわち、List<String>

また

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());
   }
}
于 2013-09-01T02:32:08.783 に答える