Write a program that asks the user to enter a name and a gen

Write a program that asks the user to enter a name and a gender, and then reads the contents of the two files into two separate arrays, or ArrayLists. The program should search the arrays/ArrayLists for the name and then display a message indicating whether the name was among the most popular. The user should enter done as a name to exit the program.

Solution

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class ReadValidateName {

   public static void main(String[] args) {
       String name, gender;
       Scanner scanner = null;
       try {

           do {
               scanner = new Scanner(System.in);
               System.out.print(\"Enter the name:\");
               name = scanner.next();
               if (name.equalsIgnoreCase(\"done\"))
                   break;

               System.out.print(\"Enter the Gender:\");
               gender = scanner.next();
               List<String> namesMales = new ArrayList<String>();
               List<String> namesFemales = new ArrayList<String>();
               scanner = new Scanner(new File(\"popularnamesmales.txt\"));
               while (scanner.hasNext()) {
                   namesMales.add(scanner.next());

               }

               scanner = new Scanner(new File(\"popularnamesfemales.txt\"));
               while (scanner.hasNext()) {
                   namesFemales.add(scanner.next());

               }
               boolean flag = false;
               Iterator<String> iterator = namesMales.iterator();
               while (iterator.hasNext()) {
                   String popularName = iterator.next();
                   if (popularName.equalsIgnoreCase(name)) {
                       System.out.println(name + \" is popular\");
                       flag = true;
                       break;
                   }

               }
               if (!flag) {
                   iterator = namesFemales.iterator();
                   while (iterator.hasNext()) {
                       String popularName = iterator.next();
                       if (popularName.equalsIgnoreCase(name)) {
                           System.out.println(name + \" is popular\");
                           flag = true;
                           break;
                       }

                   }

               }

           } while (true);

       } catch (Exception e) {
           // TODO: handle exception
       }
   }
}

popularnamesfemales.txt

Laxmi
Kumari
Madhavi
Akki

popularnamesmales.txt

Srinivas
Rajesh
Pavan
Chakri
Neehaal

OUTPUT:

Enter the name:Srinivas
Enter the Gender:Male
Srinivas is popular
Enter the name:Akki
Enter the Gender:Female
Akki is popular
Enter the name:Done

Write a program that asks the user to enter a name and a gender, and then reads the contents of the two files into two separate arrays, or ArrayLists. The progr
Write a program that asks the user to enter a name and a gender, and then reads the contents of the two files into two separate arrays, or ArrayLists. The progr

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site