Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileReader;
  3. import java.util.NoSuchElementException;
  4. import java.util.Scanner;
  5. import java.io.FileNotFoundException;
  6.  
  7. public class Exercise01 {
  8.     public static void main(String[] args) throws FileNotFoundException {
  9.         search_name();
  10.     }
  11.  
  12.     // searches for name based on user input
  13.     public static void search_name() throws FileNotFoundException {
  14.         String name;
  15.         String gender;
  16.         int count = 0;
  17.         int population;
  18.         double percentage_of_population;
  19.        
  20.         Scanner enter_name = new Scanner(System.in);
  21.         System.out.print("Choose a name to search for: ");
  22.         name = enter_name.nextLine();
  23.         Scanner enter_gender = new Scanner(System.in);
  24.         System.out.print("Choose a gender to search for: ");
  25.         gender = enter_gender.nextLine();
  26.        
  27.         double sum = 0.0;
  28.         // loops through based on years. It should move on to the txt file with the associated year.
  29.         for (int year = 1880; year <= 1886; year++) {
  30.             String file_name = "yob" + year + ".txt";
  31.             Scanner sc = new Scanner(new FileReader(file_name));
  32.  
  33.             // loops based on if there is a line that it can read
  34.             while (sc.hasNextLine())  {
  35.                 Scanner line_scanner = new Scanner(sc.nextLine());
  36.                 line_scanner.useDelimiter(",");
  37.                 String temp_name = line_scanner.next();
  38.                 String temp_gender = line_scanner.next();
  39.                 population = line_scanner.nextInt();
  40.                 sum = sum + population;
  41.                
  42.                 // now this should evaluate whether the name inputted is in the line. I use Alice and F for my tests
  43.                 if (sc.nextLine().contains(name) && sc.nextLine().contains(gender)) {
  44.                     //System.out.println(temp_name + " " + temp_gender + " " + population);
  45.                     count = count + 1;
  46.                     System.out.println("I found " + name);
  47.                 }
  48.                 //if not then it should just print that the name wasn't found
  49.                 else {
  50.                     System.out.println(name + ":" + "Name not found");
  51.  
  52.                 }
  53.                
  54.                 percentage_of_population = (count / sum) * 100;
  55.                 System.out.println("Total population count for " + year + " is " + sum);
  56.                 System.out.println("I found " + name + " " + count + " Times");
  57.                 System.out.println("Percentage of people with the name " + name + " is " + percentage_of_population);
  58.             }
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement