public int getRank(int auxYear, String auxName){
//FileResource auxFr = new FileResource("/testing/yob" + auxYear + "short.csv");
String resourceName = "/Users/User/Desktop/coursera/week4_babybirths/babybirths/testing/yob" + auxYear + "short.csv";
File auxFile = new File(resourceName);
if(auxFile.exists()){
FileResource auxFr = new FileResource(auxFile);
//FileResource auxFr = new FileResource();
int auxRank = 0;
for (CSVRecord auxRec : auxFr.getCSVParser(false)){
if (auxRec.get(1).contains(auxGender)){
auxRank += 1;
String auxN = auxRec.get(0);
if (auxRec.get(0).contains(auxName)){
return auxRank;
}
}
}
}
getName方法正在读取另一个csv文件,而不是从代码下面的第一行开始:
public String getName(int auxYear, int auxRank, String auxGender){
////FileResource auxFr = new FileResource("/testing/yob" + auxYear + "short.csv");
String resourceName = "/Users/User/Desktop/coursera/week4_babybirths/babybirths/testing/yob" + auxYear + "short.csv";
File auxFile = new File(resourceName);
if (auxFile.exists()){
FileResource auxFr = new FileResource(auxFile);
int auxCount = 0;
for (CSVRecord auxRec : auxFr.getCSVParser()){
String auxStr = auxRec.get(0);
if (auxRec.get(1).contains(auxGender)){
auxCount += 1;
String auxStr1 = auxRec.get(0);
if (auxCount == (auxRank-1)){
return auxRec.get(0);
}
}
}
}
我也用:
public String yourNameInYear(String auxName, int auxYear, int auxNewYear, String auxGender){
int auxRank = getRank(auxYear, auxName, auxGender);
return getName(auxNewYear, auxRank, auxGender);
}
public void testYourNameInYear(){
String auxName = yourNameInYear("Isabella", 2012, 2014, "F");
System.out.println("Isabella" + " born in " + 2012 + " would be " + auxName + " in " + 2014);
}
默认情况下YourNameInYear
会调用getRank
这个会打开yob2012short.csv
,然后在处理后,getName
会被调用并打开yob2014short.csv
。我不知道为什么这个不是从第一行开始的?
答案 0 :(得分:0)
尝试使用此
if (auxRec.get(1).contains(auxGender)){
String auxStr1 = auxRec.get(0);
if (auxCount == (auxRank-1)){
return auxRec.get(0);
}
auxCount += 1;
}
因为我gess auxCaunt
必须是您的current row
所以如果您将其从0
增加到1
,那么您确实使用auxRank
等于调用getName到0
(第一行可能是0而不是1)因此0
不等于1
所以它会循环直到auxRank
等于2