Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 10g Insider Solutions

Authors: Arun R. Kumar, John Kanagaraj, Richard Stroupe

1st Edition

0672327910, 978-0672327919

More Books

Students also viewed these Databases questions

Question

4-6 Is there a digital divide? If so, why does it matter?

Answered: 1 week ago