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

My program not reading from the file txt and there is a problem when it is displ

ID: 3736214 • Letter: M

Question

My program not reading from the file txt and there is a problem when it is display the output is not give me the ranking for the name for both gender: for example if we shoice from 2001 and chose the gender m it is not reading my input the right way and it is not reading from my files. And its not give the user his input is not right or something like that:

could you please check my code:

/**

* @author sameer

* to prompt the detailsfrom the user

* year, gender and name and these paramters are passed to the

* search file method and rank is returned from that method

* once the rank is returned, the user is asked again whether

* to search for any other baby names or not. If yes again details

* will be requested or else the user input will be blocked using

* System.exit(0)

*/

import java.io.BufferedReader;

import java.io.FileReader;

import java.util.*;

public class babyNamePopularityRanking {

Scanner input; // scanner object to get the input from the user

public void showPrompt(){

input = new Scanner(System.in);

System.out.print(" Enter the year : ");

String year = input.next();

System.out.print(" Enter the gender : ");

String gender = input.next();

System.out.print(" Enter the name : ");

String name = input.next();

String rank = searchFile(year,gender,name);

String sex = gender.equalsIgnoreCase("M")?"Boy":"Girl";

System.out.println(sex + " name " + name + " is ranked #" + rank + " in year " + year);

System.out.print(" Enter another inquiry? ");

String anotherEnquiry = input.next();

if(anotherEnquiry.equalsIgnoreCase("Y")) {

showPrompt();

} else{

System.exit(0);

}

}

/**

* @param year - year need to be searched

* param gender - gender of the baby

* param name - name of the baby

* return the rank of the name

* here based on gender the rank will be displayed

* if the gender name is M the split position with 1 is compared

* if the gender name is F the split position with 3 is compared

*/

public String searchFile(String year, String gender, String name){

String rank = null; // to return the rank of the name

BufferedReader reader = null;

String line = "";

try{

reader = new BufferedReader(new FileReader("babynameranking" + year.trim() + "txt"));

// in this while loop based on the gender type

// the splitted characters will be compared

while ((line = reader.readLine()) != null) {

String data = line;

String split[] = data.split("");

if(gender.equalsIgnoreCase("M")){

if(name.equalsIgnoreCase(split[1])){

rank = split[0];

}

}else{

if(name.equalsIgnoreCase(split[3])){

rank = split[0];

}

}

}

reader.close();

}catch (Exception e) {

}

return rank;

}

/**

* @param args

* here the program is started

*/

public static void main(String[] args){

babyNamePopularityRanking babyNames = new babyNamePopularityRanking();

babyNames.showPrompt();

}

}

Explanation / Answer

import java.io.*;

import java.util.*;

public class babyNamePopularityRanking {

   BufferedReader input; // BufferedReader object to get the input from the user
String gender,year,name;

public void showPrompt()
   {
       try{
    input=new BufferedReader(new InputStreamReader(System.in));
   System.out.print(" Enter the year : ");
   year =input.readLine();
   System.out.print(" Enter the gender : ");
   gender = input.next();
   System.out.print(" Enter the name : ");
   name = input.next();
   String rank = searchFile(year,gender,name);
   String sex = gender.equalsIgnoreCase("M")?"Boy":"Girl";
   System.out.println(sex + " name " + name + " is ranked #" + rank + " in year " + year);
   System.out.print(" Enter another inquiry? ");
   String anotherEnquiry = input.next();
  
   if(anotherEnquiry.equalsIgnoreCase("Y"))
           showPrompt();
    else
           System.exit(0);
       }
       catch(Exception e){}

   }


   public String searchFile(String year, String gender, String name){

       String rank = null; // to return the rank of the name
       BufferedReader reader = null;
       String line = "";
       try{
         reader = new BufferedReader(new FileReader("babynameranking" + year.trim() + ".txt"));
       while ((line = reader.readLine()) != null)
       {
               String data = line;
               String split[] = data.split("");
               if(gender.equalsIgnoreCase("M"))
                   {
                   if(name.equalsIgnoreCase(split[1]))
                           rank = split[0];
                                                      
                    }
           else{
               if(name.equalsIgnoreCase(split[3]))
                       rank = split[0];

               }
       }

reader.close();

}catch (Exception e) {

}

return rank;

}

public static void main(String[] args){

   babyNamePopularityRanking babyNames = new babyNamePopularityRanking();

   babyNames.showPrompt();

                                   }

}

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