In Java please. Chapter on Exception Handling and Text I/O 2.31aby name populari
ID: 3766701 • Letter: I
Question
In Java please. Chapter on Exception Handling and Text I/O 2.31aby name popularity runking) The popularity ranking of baby names from stored * 12.31 (Baby name popu in files named babynameranking2001.txt, babynameranking2002.ttod pabynameranking2010.txt. Each file contains one thousand lines. Each line babynar contains a ranking, a boy's name, number for the boy's name, a girl's na years 2001 to 2010 is downloaded from www.ssa.gov/oact/babynames and s s name, o lines in the file and number for the girl's name. For example, the first two lines in the f babynameranking2010.txt are as follows: 21,875 17,866 Isabella Sophia 22,731 20,477 Jacob Ethan So, the boy's name Jacob and girl's name Isabella are ranked #1 and the boy's name Ethan and girl's name Sophia are ranked #2. 21,875 boys are named Jacob and 22,731 girls are named Isabella. Write a program that prompts the user to enter the year, gender, and followed by a name, and displays the ranking of the name for the year. Here is a sample run: Enter the year: 2010 Enter Enter the gender: M Enter Enter the name: Javier Javier is ranked #190 in year 2010 Enter the year: 2010 Entr Enter the gender: F Enter Enter the name: ABC Enter The name ABC is not ranked in year 2010
Explanation / Answer
The code is given below. Please make changes in the input file path to run on your system.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* using NetBeans
* @author anurag
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class NewClass {
public static void main(String args[]) throws FileNotFoundException
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the year:");
int year=sc.nextInt();
System.out.println();
System.out.print("Enter the Gender:");
String gender=sc.next();
System.out.println();
System.out.print("Enter the Name:");
String name=sc.next();
System.out.println();
// give your input files path below
String file="C:/Users/anurag/Desktop/babynameranking"+year+".txt";
// use try catch to handle FileNotFound exception
try{
File file1 = new File(file); // new file object
final Scanner scanner = new Scanner(file1);
int flag=0;
while (scanner.hasNextLine()) {
final String line_File = scanner.nextLine(); // reading next line
String[] words = line_File.trim().split(" +"); // splitting the line into desired words
if(gender.equals("M")&& words[1].equals(name)) // checking conditions for Male name
{
flag=1;
System.out.println(name+ " is ranked #"+words[0]+" in year "+year);
// printing the output
break;
}
else if(gender.equals("F")&& words[3].equals(name)) {
flag=1;
// printing the output
System.out.println("The name "+name+ " is ranked #"+words[0]+" in year "+year);
break;
}
}
if(flag==0)
{
System.out.println("The name "+name+" is not ranked in year "+year);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Input:
babynameranking2001.txt
1 anurag 2000 shivani 2001
2 deepak 1000 shweta 1999
3 sandeep 900 divya 1000
4 pramod 850 priti 900
5 bob 800 Alice 899
output:
Enter the year:2001
Enter the Gender:M
Enter the Name:anurag
anurag is ranked #1 in year 2001
BUILD SUCCESSFUL (total time: 12 seconds)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.