I am taking this Java course with Duke Uni on Coursera. We are using BlueJ.
The data used is a csv with 10 rows and 3 cols.
I have a method that works fine alone called getRank, it effectively just gives me the line number of a name I want in a csv. The tweak is that Males and Females are in the same column, so it starts with females, and then it comes to males, so I need to restart the line count for males when I reach them to get the correct rank (i.e. Sophia has rank 1, Jacob also has rank 1). The working method code is:
public int getRank (int year, String name, String gender) {
String filename = "yob"+year+"short.csv";
FileResource fr = new FileResource(filename);
int rank = -1;
int count = 0;
int first_female = 0;
for (CSVRecord rec : fr.getCSVParser(false)){
count += 1;
if (rec.get(1).equals("F")){
first_female += 1;
}
if (rec.get(1).equals(gender) && rec.get(0).equals(name)){
rank = count;
}
}
if (gender == "M"){
rank = rank - first_female;
}
return rank;
}
But now I am trying to make another method called getTotalBirthsRankedHigher, which uses the third column in the data to make a cumulative sum for all names with ranks higher than the input name (so Ethan should have 8+7 = 15, bc Jacob & Mason are higher ranked, so we sum them).
The method I wrote calls getRank() but getRank is not discriminating between males and females anymore, so I am having mistakes when inputing a male name. The code is:
public void getTotalBirthsRankedHigher (int year, String name, String gender){
int higher_births = 0;
int names_rank = getRank(year, name, gender);
String rec_name = "";
String rec_gen = "";
int current_record_rank = 0;
String filename = "yob"+year+"short.csv";
FileResource fr = new FileResource(filename);
for (CSVRecord rec : fr.getCSVParser(false)){
rec_name = rec.get(0);
rec_gen = rec.get(1);
current_record_rank = getRank(year, rec_name, rec_gen);
System.out.println(year + rec_name + rec_gen + current_record_rank);
if (rec.get(0) != name && rec.get(1).equals(gender) && names_rank > current_record_rank){
higher_births += Integer.parseInt(rec.get(2));
}
}
System.out.println("The total number of births higher than " + name + " in the year: " + year + " is "+ higher_births);
}
What am I doing wrong?