我正在Coursera Java课程中进行作业。作业是有关Java中的统计信息的,他们给了我一些文件,其中包含每个名字的出生人数,并且想要每个名字的排名,例如hesham 15,ahmed 12和ibrahim 10,因此hesham分别排名1和ahmed no 2。依其排名排序的档案(CSV档案):Hesham M 15,然后是Ahmed M 12,依此类推。女性首先是女性。我在getAverage上工作,它按预期工作,但是当我尝试以其他方法使用它时,它并没有给我带来预期的结果,请当我遇到问题时请帮助我。这是两种方法的代码

public int getRank(int year, String name, String gender, FileResource fr) {
    int rank = 0;
    int colNum = 0;
    boolean found = false;
    fr = new FileResource();
    for (CSVRecord record : fr.getCSVParser(false)) {
        if (record.get(0).equals(name) && record.get(1).equals(gender))
            found = true;
    }
    for (CSVRecord record : fr.getCSVParser(false)) {
        colNum++;
    }
    for (CSVRecord record : fr.getCSVParser(false)) {
        rank++;
        if (record.get(0).equals(name) && record.get(1).equals(gender))
            break;
    }
    if (found == true) {
        if (gender.equals("F"))
            return rank;
        else
            return rank = rank - (colNum / 2);
    }
    else
        return -1;
}




double getAverageRank(String name, String gender) {
    double average = 0;
    int rank = 0;
    int counter = 0;
    int year = 0;
    String namem = null;
    DirectoryResource dr = new DirectoryResource();
    for (File f : dr.selectedFiles()) {
        FileResource fr = new FileResource(f);
        counter++;
        System.out.println("Counter is " + counter);
        name = f.getName();
        System.out.println("name of file  is " + name);
        int index = name.indexOf("yob", 0);
        namem = name.substring(index + 3, index + 7);
        System.out.println("name of year is " + namem);
        year = Integer.parseInt(namem);
        int currrank = getRank(year, name, gender, fr);
        System.out.println("current rank is " + currrank);
        rank = currrank + rank;
        System.out.println("rank final is " + rank);
    }
    return rank / counter;
}

最佳答案

您的实现存在两个主要问题:


您用年份覆盖了名称变量,因此将文件名传递给getRank(),而不是您要查找的名称。
在getRank中,执行fr = new FileResource();。不要创建新的FileResource,使用获得的FileResource作为参数


getRank()比您想象的要简单得多,这应该可以修复您的实现:

// fr holds your file, so all you have to do is:
// - look each line of the file until you find the name and gender
// - return the value of the 3rd column
public int getRank(int year, String name, String gender, FileResource fr) {
    for (CSVRecord record : fr.getCSVParser(false)) {
        if (record.get(0).equals(name) && record.get(1).equals(gender)) {
            return Integer.parseInt(record.get(2));
        }
    }
    return -1;
}




double getAverageRank(String name, String gender) {
    DirectoryResource dr = new DirectoryResource();

    int rank = 0;
    int counter = 0;
    for (File f : dr.selectedFiles()) {
        FileResource fr = new FileResource(f);

        counter++;
        System.out.println("Counter is " + counter);

        // here do not use the name param to hold the file name, you will lose the name you are looking for
        // instead create a new variable, here fileName
        String fileName = f.getName();
        System.out.println("name of file  is " + fileName);

        int index = fileName.indexOf("yob", 0);
        String yearAsString = fileName.substring(index + 3, index + 7);
        System.out.println("name of year is " + yearAsString);
        int year = Integer.parseInt(yearAsString);

        int currrank = getRank(year, name, gender, fr);
        System.out.println("Rank in current file is " + currrank);

        rank = currrank + rank;
        System.out.println("Rank total is " + rank);
    }

    int averageRank = rank / counter;
    System.out.println("Average rank = " + averageRank);
    return averageRank;
}


注意:

getRank()接收年份,但是我们不使用它,因此即使这有效,我认为这并不是他们想要的实现。但是,我无法理解getRank既要接收年份又要接收FileResource的必要性,我认为这是其中之一。

我们可以有

public int getRank(String name, String gender, FileResource fr)


而且仍然可以

09-26 08:23