13.7k views
2 votes
Write a program that reads the file and displays the names that are used for both genders in the file. Use exception handling for graceful flow/termination. Here is a sample run: Enter a file name for baby name ranking: 69 names used for both genders. They are Tyler, Ryan, Christian, ...

User Ajt
by
8.6k points

1 Answer

3 votes

Final answer:

To write a program that reads a file and displays names used for both genders, you can use exception handling and follow a step-by-step process. The program will prompt the user for a file name, read the file, and check each name in both male and female lists to identify names used for both genders.

Step-by-step explanation:

To solve this problem, we can follow these steps:

  1. Prompt the user to enter a file name for baby name ranking.
  2. Use exception handling to handle any errors that may occur when reading the file.
  3. Read the file and store the names in separate lists for male and female names.
  4. Create an empty list to store the names that are used for both genders.
  5. Iterate through one of the lists and check if each name is present in the other list.
  6. If a name is found in both lists, add it to the list of names used for both genders.
  7. Display the total number of names used for both genders and the names themselves.

For example, if the user enters 'baby_names.txt' as the file name and the file contains names like 'Tyler' (used for both genders), 'Emma' (female name), and 'Michael' (male name), the program would display '69 names used for both genders. They are Tyler, Ryan, Christian, ...'.

User Kessy
by
8.5k points