Question
in the method getAveragerank it can't find the variable for file and it says continue is outside of the loop. Thank You. import edu.duke.DirectoryResource; import
in the method getAveragerank it can't find the variable for file and it says continue is outside of the loop.
Thank You.
import edu.duke.DirectoryResource;
import java.io.File;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import edu.duke.FileResource;
public class BabyBirths {
public void printNames () {
FileResource fr = new FileResource();
for (CSVRecord rec : fr.getCSVParser(false)) {
int numBorn = Integer.parseInt(rec.get(2));
if (numBorn <= 100) {
System.out.println("Name " + rec.get(0) +
" Gender " + rec.get(1) +
" Num Born " + rec.get(2));
}
}
}
public void totalBirths (FileResource fr) {
int totalBirths = 0;
int totalBoys = 0;
int totalGirls = 0;
for (CSVRecord rec : fr.getCSVParser(false)) {
int numBorn = Integer.parseInt(rec.get(2));
totalBirths += numBorn;
if (rec.get(1).equals("M")) {
totalBoys += numBorn;
}
else {
totalGirls += numBorn;
}
}
System.out.println("total births = " + totalBirths);
System.out.println("female girls = " + totalGirls);
System.out.println("male boys = " + totalBoys);
}
public void testTotalBirths () {
//FileResource fr = new FileResource();
FileResource fr = new FileResource("data/yob2014.csv");
totalBirths(fr);
}
public int getRank(CSVParser parser, String name, String Gender ) {
int rank = 0;
FileResource fr = new FileResource();
for (CSVRecord rec: fr.getCSVParser(false)) {
if (rec.get(1).equals("Gender")); {
rank++;
if (rec.get(0).equals("Name")); {
return rank;
}
}
}
return -1;
}
public String getName(int year, int rank, String gender ) {
FileResource fr = new FileResource();
for (CSVRecord rec : fr.getCSVParser(false)) {
String name = rec.get(0);
if(rank == getRank(year , name , gender)) {
return name;
}
}
return "No Name";
}
private CSVParser parserFactory(int year) {
final String path = "C:/Users/pd/Documents/Coursera/duke-java-1/BabyNames/data/us_babynames_by_year/";
String fileName = path+"yob"+year+".csv";
FileResource fr = new FileResource(fileName);
return fr.getCSVParser(false);
}
/**
* Helper factory for creating CSVParser from file
* @param file file to be parsed
* @retirm CSVParser from file
*/
private CSVParser parserFactory(File file) {
FileResource fr = new FileResource(file);
return fr.getCSVParser(false);
}
public void whatNameInYear(String name, int year, int nyear, String gender) {
int namerank = getRank(year, name, gender);
String newName = getName(nyear, namerank,gender);
if (gender.equals("m")) {
System.out.println(name + "born in" + year + "would be" +newName + "if he was born in" + nyear + ".");
System.out.println(name + "born in" + year + "would be" +newName + "if she was born in" + nyear + ".");
}
}
public int yearOfHighestRank(String name, String gender) {
DirectoryResource dr = new DirectoryResource();
int highestsofar = -1;
int yearofrank = -1;
String fileName;
for( File file: dr.selectedFiles()) {
String fName = file.getName();
int year = findYear(fName);
int rank = getRank(year, name, gender);
// continue if rank not found in file
if(rank == -1) continue;
// for the first found rank
if(highestsofar == -1) {
highestsofar = rank;
yearofrank = year;
}
}
return yearofrank;
}
private int findYear(String fName) {
// yob 2014 short.csv
// 012 3456 789
return Integer.parseInt(fName.substring(3, 7));
}
public double getAverageRank(String name, String gender ) {
DirectoryResource dr = new DirectoryResource();
int rankSum = 0;
int rankCount = 0;
for (File file : dr.selectedFiles()); {
CSVParser parser = parserFactory(file);
int rank = getRank(parser, name, gender);
if (rank == -1) continue;
rankSum += rank;
rankCount++;
}
if(rankCount == 0) return -1.0d;
else return rankSum/(double)rankCount;
}
public int getRank(int year, String name, String gender) {
CSVParser parser = parserFactory(year);
return getRank(parser, name, gender);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started