Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name:Nate Wheeler
- * Date: Nov. 4, 2016
- * Course Number: csc220
- * Course Name: data structures
- * Problem Number: hw 6
- * Email: nate23nate23@gmail.com
- * Short Description of the Problem:
- * make a list of top baby names in a given year
- */
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.Scanner;
- public class ReadBabyNames {
- private static void process(Scanner scr, String args[]) throws FileNotFoundException, IllegalArgumentException {
- // Code here is merely a sample
- ArrayList<BabyName> boys = new ArrayList<BabyName>();
- ArrayList<BabyName> girls = new ArrayList<BabyName>();
- ArrayList<BabyName> list;
- Scanner entry = new Scanner(System.in);
- System.out.println("Baby Names of America!\n Please choose between years 1880 to 2015\n From:");
- int year1 =entry.nextInt();
- if(year1>2015 || year1<1880)
- throw new IllegalArgumentException("not within the permits years, try again.");
- System.out.println("To: ");
- int year2= entry.nextInt();
- if(year1>2015 || year1<1880)
- throw new IllegalArgumentException("not within the permits years");
- if(year1>year2){
- int a=year1;
- year1=year2;
- year2=a;
- System.out.println("Switching years and reading from "+year1+"to "+year2);
- }
- System.out.println("Number of Baby Name Finalist:");
- int finalist=entry.nextInt();
- if(finalist<=0)
- throw new IllegalArgumentException("please choose a number between 1 and 2000");
- while(year1<=year2){
- //Scanner sc = new Scanner(new File("babynamedata/yob1880.txt"));
- Scanner sc = new Scanner(new File("babynamedata/yob"+year1+".txt"));
- sc.useDelimiter("\\s*,\\s*|\\s+");
- while (sc.hasNextLine()) {
- String name = sc.next();
- String sex = sc.next();
- int number = sc.nextInt();
- sc.nextLine();
- // System.out.printf("%15s%2s%10d\n", name, sex, number);
- BabyName baby = new BabyName(name, number);
- if(sex.equals("M"))
- list=boys;
- else
- list=girls;
- int index = list.indexOf(baby);
- if(index==-1)
- list.add(baby);
- else
- list.get(index).addToNumber(number);
- }
- sc.close();
- year1++;
- }
- Collections.sort(boys);
- System.out.println("Top Names for Boys:");
- for(int a=0; a<=finalist-1;a++){
- System.out.println(boys.get(a));
- }
- Collections.sort(girls);
- System.out.println("\nTop Names for Girls:");
- for(int a=0; a<=finalist-1;a++){
- System.out.println(girls.get(a));
- }
- entry.nextLine(); // IMPORTANT!! Reset Scanner
- //System.out.println("Processing " + + " ...");
- }
- private static boolean doThisAgain(Scanner sc, String prompt) {
- System.out.print(prompt);
- String doOver = sc.nextLine();
- return doOver.equalsIgnoreCase("Y");
- }
- //**********************************************
- public static void main(String args[]) throws FileNotFoundException {
- final String TITLE = "CSC220 BabyNames";
- final String CONTINUE_PROMPT = "Do this again? [y/N] ";
- System.out.println("Welcome to " + TITLE);
- Scanner scan = new Scanner(System.in);
- do {
- process(scan, args);
- } while (doThisAgain(scan, CONTINUE_PROMPT));
- scan.close();
- System.out.println("Thank you for using " + TITLE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement