<commit_before>/*
  Revise Programming Exercise 21.11 to prompt the user to enter year, gender,
  and name and display the ranking for the name. Prompt the user to enter
  another inquiry or exit the program.
*/

import java.util.Scanner;
import java.io.IOException;

public class E21_13 {
  public static void main(String[] args) throws IOException {
    BabyNames bn = new BabyNames();

    Scanner input = new Scanner(System.in);
    do {
      System.out.print("Enter the year: ");
      int year = input.nextInt();

      if (year < 2001 || year > 2010) {
        System.out.println("Year must be between 2001 and 2010");
        continue;
      }

      int index = getIndexForYear(year); // get the array index for year

      System.out.print("Enter the gender (M/F): ");
      String gender = input.next().toLowerCase();

      if (!gender.equals("m") && !gender.equals("f")) {
        System.out.println("Please enter either M or F for gender");
        continue;
      }

      System.out.print("Enter the name: ");
      String name = input.next();

      if (gender.equals("m")) {
        if (bn.getBoyData().get(index).get(name) != null) {
          String rank = bn.getBoyData().get(index).get(name);
          System.out.println("Boy name " + name + " is ranked #" + rank +
            " in year " + year);
        } else {
          System.out.println(name + " not found");
        }
      } else if (gender.equals("f")) {
        if (bn.getGirlData().get(index).get(name) != null) {
          String rank = bn.getGirlData().get(index).get(name);
          System.out.println("Girl name " + name + " is ranked #" + rank +
            " in year " + year);
        } else {
          System.out.println(name + " not found");
        }
      }

      System.out.print("Enter another inquiry? (N to exit): ");
      if (input.next().toLowerCase().equals("n")) {
        System.exit(1);
      }

    } while (true);
  }

  public static int getIndexForYear(int year) {
    for (int i = 0, j = 2001; i < 10; i++, j++) {
      if (j == year) { return i; }
    }<commit_msg>Refactor result printing into new method<commit_after>/*
  Revise Programming Exercise 21.11 to prompt the user to enter year, gender,
  and name and display the ranking for the name. Prompt the user to enter
  another inquiry or exit the program.
*/

import java.util.Scanner;
import java.io.IOException;

public class E21_13 {
  public static void main(String[] args) throws IOException {
    BabyNames bn = new BabyNames();

    Scanner input = new Scanner(System.in);
    do {
      System.out.print("Enter the year: ");
      int year = input.nextInt();

      if (year < 2001 || year > 2010) {
        System.out.println("Year must be between 2001 and 2010");
        continue;
      }

      System.out.print("Enter the gender (M/F): ");
      String gender = input.next().toLowerCase();

      if (!gender.equals("m") && !gender.equals("f")) {
        System.out.println("Please enter either M or F for gender");
        continue;
      }

      System.out.print("Enter the name: ");
      String name = input.next();

      printRanking(bn, year, gender, name);

      System.out.print("Enter another inquiry? (N to exit): ");
      if (input.next().toLowerCase().equals("n")) {
        System.exit(1);
      }

    } while (true);
  }

  private static void printRanking(
    BabyNames bn, int year, String gender, String name) {
    int index = getIndexForYear(year);
    String rank = "";
    StringBuilder result = new StringBuilder();
    if (gender.equals("m")) {
      rank = bn.getBoyData().get(index).get(name);
      result.append("Boy ");
    } else {
      rank = bn.getGirlData().get(index).get(name);
      result.append("Girl ");
    }
    if (rank == null) {
      System.out.println("Name not found ");
      return;
    }
    result.append("name " + name + " is ranked #" + rank + " in year " + year);
    System.out.println(result);
  }

  private static int getIndexForYear(int year) {
    for (int i = 0, j = 2001; i < 10; i++, j++) {
      if (j == year) { return i; }
    }
{'fraction_non_alphanumeric': 0.14603718199608612, 'fraction_numerical': 0.015655577299412915, 'mean_word_length': 2.4075, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2808931', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1223, 'n_words': 369}