Advertisement
nate23nate23

hw6 main -works-ish

Nov 4th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 KB | None | 0 0
  1. /*
  2.  * Name:Nate Wheeler
  3.  * Date: Nov. 4, 2016
  4.  * Course Number: csc220
  5.  * Course Name: data structures
  6.  * Problem Number: hw 6
  7.  * Email: nate23nate23@gmail.com
  8.  * Short Description of the Problem:
  9.  * make a list of top baby names in a given year
  10.  */
  11. import java.io.File;
  12. import java.io.FileNotFoundException;
  13. import java.util.ArrayList;
  14. import java.util.Arrays;
  15. import java.util.Collections;
  16. import java.util.Scanner;
  17.  
  18. public class ReadBabyNames {
  19.    
  20.     private static void process(Scanner scr, String args[]) throws FileNotFoundException, IllegalArgumentException {
  21.     // Code here is merely a sample
  22.         ArrayList<BabyName> boys = new ArrayList<BabyName>();
  23.         ArrayList<BabyName> girls = new ArrayList<BabyName>();
  24.         ArrayList<BabyName> list;
  25.         Scanner entry = new Scanner(System.in);
  26.         System.out.println("Baby Names of America!\n Please choose between years 1880 to 2015\n From:");
  27.         int year1 =entry.nextInt();
  28.         if(year1>2015 || year1<1880)
  29.             throw new IllegalArgumentException("not within the permits years, try again.");
  30.         System.out.println("To: ");
  31.         int year2= entry.nextInt();
  32.         if(year1>2015 || year1<1880)
  33.             throw new IllegalArgumentException("not within the permits years");
  34.  
  35.         if(year1>year2){
  36.             int a=year1;
  37.             year1=year2;
  38.             year2=a;
  39.             System.out.println("Switching years and reading from "+year1+"to "+year2);
  40.         }
  41.         System.out.println("Number of Baby Name Finalist:");
  42.         int finalist=entry.nextInt();
  43.         if(finalist<=0)
  44.         throw new IllegalArgumentException("please choose a number between 1 and 2000");
  45.         while(year1<=year2){
  46.         //Scanner sc = new Scanner(new File("babynamedata/yob1880.txt"));
  47.         Scanner sc = new Scanner(new File("babynamedata/yob"+year1+".txt"));
  48.         sc.useDelimiter("\\s*,\\s*|\\s+");
  49.         while (sc.hasNextLine()) {
  50.             String name = sc.next();
  51.             String sex = sc.next();
  52.             int number = sc.nextInt();
  53.             sc.nextLine();
  54. //          System.out.printf("%15s%2s%10d\n", name, sex, number);
  55.             BabyName baby = new BabyName(name, number);
  56.             if(sex.equals("M"))
  57.                 list=boys;
  58.             else
  59.                 list=girls;
  60.             int index = list.indexOf(baby);
  61.             if(index==-1)
  62.                 list.add(baby);
  63.             else
  64.                 list.get(index).addToNumber(number);
  65.            
  66.        
  67.         }
  68.        
  69.        
  70.         sc.close();
  71.         year1++;
  72.         }
  73.         Collections.sort(boys);
  74.         System.out.println("Top Names for Boys:");
  75.         for(int a=0; a<=finalist-1;a++){
  76.             System.out.println(boys.get(a));
  77.         }
  78.         Collections.sort(girls);
  79.         System.out.println("\nTop Names for Girls:");
  80.         for(int a=0; a<=finalist-1;a++){
  81.             System.out.println(girls.get(a));
  82.         }      
  83.     entry.nextLine();  // IMPORTANT!! Reset Scanner
  84.     //System.out.println("Processing " +  + " ...");
  85. }
  86.  
  87.  
  88.  
  89. private static boolean doThisAgain(Scanner sc, String prompt) {
  90.     System.out.print(prompt);
  91.     String doOver = sc.nextLine();
  92.     return doOver.equalsIgnoreCase("Y");
  93. }
  94.  
  95. //**********************************************
  96.  
  97.  
  98.  
  99.  
  100.  
  101.     public static void main(String args[]) throws FileNotFoundException {
  102.         final String TITLE = "CSC220 BabyNames";
  103.         final String CONTINUE_PROMPT = "Do this again? [y/N] ";
  104.         System.out.println("Welcome to " + TITLE);
  105.         Scanner scan = new Scanner(System.in);
  106.         do {
  107.             process(scan, args);
  108.         } while (doThisAgain(scan, CONTINUE_PROMPT));
  109.         scan.close();
  110.         System.out.println("Thank you for using " + TITLE);
  111.        
  112.     }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement