Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java application that reads two files into separate ArrayLists. Once you

ID: 3767918 • Letter: W

Question

Write a Java application that reads two files into separate ArrayLists. Once you have read and stored the names prompt the user to enter in a boy’s name and a girl’s name. Check to see if the name is in the ArrayList and notify the user accordingly. Assuming the files are sorted from most popular to least popular also display the “rank” of the name if it is found. (Hint: remember the indices of an array are off by one). You can do all of this work in the main method. You can hardcode the names of the files.

A more elegant solution would be to use methods to break up these tasks. Implement the following:

readNameFile: takes a string (the name of the file) as an argument and returns an ArrayList with 200 names. Note: all file I/O is done inside of this method.

searchForName: takes an ArrayList and a String as arguments. Returns the index of the name if it is in the ArrayList, -1 if it is not. Use linear search to accomplish this task.

Execution of program should look like this:

java Names

Please enter one girl and one boy name separated by a new line:

Suzie
James

Suzie was not found in the list of 200 most popular girl names. James was found, and was ranked 17 by popularity.

Explanation / Answer

Please find the required solution:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FindPopular {
  
   // Method that reads all the names from file and write add it an arraylist
   // and return it
   static List<String> readNameFile(String fileName) throws IOException {
       List<String> arrayList = new ArrayList<String>();
       FileReader freader = new FileReader(new File(fileName));
       BufferedReader breader = new BufferedReader(freader);
       String currentLine;
       while ((currentLine = breader.readLine()) != null) {
           arrayList.add(currentLine.trim());
       }
       breader.close();
       return arrayList;
   }

   // Method that returns rank if found in list else it returns -1
   static int searchForName(String searchKey, List<String> list) {
       for (int i = 0; i < list.size(); i++) {
           if (searchKey.equals(list.get(i))) {
               return i + 1;
           }
       }
       return -1;
   }

   public static void main(String[] args) throws IOException {

       Scanner input = new Scanner(System.in);
       // list for girls
       List<String> firstFileNames = readNameFile("In1.txt");
       // list for boys
       List<String> secondFileNames = readNameFile("In2.txt");

       // read input from user
       System.out
               .println("Please enter one girl and one boy name separated by a new line");
       String girlsName = input.nextLine();
       String boysName = input.nextLine();
       input.close();

       // computes ranks and print result
       int rank;
       if ((rank = searchForName(girlsName, firstFileNames)) == -1) {
           System.out.println(girlsName + " was not found in the list of "
                   + firstFileNames.size() + " most popular girl names");
       } else {
           System.out.println(girlsName + " was found, and was ranked " + rank
                   + " by popularity. ");
       }
       if ((rank = searchForName(girlsName, firstFileNames)) == -1) {
           System.out.println(boysName + " was not found in the list of "
                   + secondFileNames.size() + " most popular boys names");
       } else {
           System.out.println(boysName + " was found, and was ranked " + rank
                   + " by popularity. ");
       }

   }// end of main method
}// end of class

Please enter one girl and one boy name separated by a new line:

rams
raghu

rams was not found in the list of 20most popular girl names.

raghu was found, and was ranked 5 by popularity.

Ouput

Please enter one girl and one boy name separated by a new line:

rams
raghu

rams was not found in the list of 20most popular girl names.

raghu was found, and was ranked 5 by popularity.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote