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

Could someone help me with a part of this assignment? I\'m not sure how to do th

ID: 3701747 • Letter: C

Question

Could someone help me with a part of this assignment? I'm not sure how to do the 3 bolded parts in the assignment and would like someone to explain it too me and what methods I should be using.

ASSIGNMENT:

For this programming assignment, you will be writing software in support of a “data base” search to determine if a particular baby name was popular in a particular year. If the baby name is in the “data base,” you are to display the year, name, and number of babies given that name in that year. If the baby name is not in the “data base,” you are to display the year, name, and a short message indicating the name was not found in that year.

Program Requirements:

Use JOptionPane for ALL user input and output

Must validate ALL user input

Must include ALL necessary exception handling

ALL methods must do only one thing

Must follow ALL programming style guidelines

Program Description:

Ask the user for the baby name, the gender (some names apply to both genders) and the year to search, or to exit the program (be efficient in the use of JOptionPanes - users do not like to see too many different boxes)

Build the file name to search (the file name is yobxxx.txt, where xxx is the four-digit year) (yob means year of birth) <-

Open the file <-

Search the file for the name and gender requested <-

Display the results to the user (year, name, gender, number of babies with name), or a not found message containing the name and year searched

Close the file

Ask the user to search another name, gender, year or to quit

Explanation / Answer

Answer :

import java.io.File;

import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class BabyNames {

/**
* @param args the command line arguments
*/
String name;//Store the name of baby
String gender;//store the gender of baby
int year;//store the year of birth

void input() {
try {
name = JOptionPane.showInputDialog(null, "Enter the Baby Name", "Baby's Name", JOptionPane.INFORMATION_MESSAGE);
if (name.isEmpty() || name.matches(".*\d+.*")) {//throw an exception if an empty name has been entered or if the name contains digits from 0 to 9
//d specifies a search for any digits
//.* means that any number of characters can exist before and after digit
throw new Exception();
}
gender = JOptionPane.showInputDialog(null, "Enter Baby's Gender(F/ M)", "Gender", JOptionPane.INFORMATION_MESSAGE);
gender = gender.toUpperCase();//convert the gender to upper case before checking
if (!(gender.equals("M") || gender.equals("F"))) {//throw an exception if the entered input is not M or F
throw new Exception();
}
year = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the Birth Year", "Birth Year", JOptionPane.INFORMATION_MESSAGE));
if (year < 1996 || year > 2000) {//throw an exception if the year is not from 1996 to 2000
throw new Exception();
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Invalid Input Entered", "Invalid Input", JOptionPane.ERROR_MESSAGE);
}
}

void processName() {
boolean found = false;//check if the baby name was found
try (Scanner sc = new Scanner(new File("yob" + year + ".txt"))) {//try to open the file. If the file is opened then proceed with processing
while (sc.hasNextLine()) {//Check if there are any more lines in the file
String record = sc.nextLine();//read a line from the file
String babyDetails[] = record.split(",");//split the record using , as the delimiter
if (babyDetails[0].equalsIgnoreCase(name)//check if the name and gender details match
&& babyDetails[1].equalsIgnoreCase(gender)) {
//show the information to the user about the name
JOptionPane.showMessageDialog(null, record, babyDetails[0] + " in year " + year + " for Gender " + babyDetails[1], JOptionPane.INFORMATION_MESSAGE);
found = true;//mark found as true
}
}
sc.close();//close the stream to the file
if (!found)//if the baby's name ws not found
{
//show the message that name wasn't found
JOptionPane.showMessageDialog(null, "Cannot be Found", name + " in year " + year, JOptionPane.ERROR_MESSAGE);
}
} catch (FileNotFoundException ex) {
//throw an exception if the specified year's file cannot be found
JOptionPane.showMessageDialog(null, "The File yob" + year + ".txt cannot be found", "File Not Found", JOptionPane.ERROR_MESSAGE);
}
}

public static void main(String[] args) {
// TODO code application logic here
BabyNames baby = new BabyNames();//create an object of BabyNames class
int ch = 0;
do {
baby.input();//Call the input function
baby.processName();//call processName function
//Ask if the user wants to continue
ch = JOptionPane.showConfirmDialog(null, "Do you want to search again?", "Continue", JOptionPane.YES_NO_OPTION);
} while (ch != JOptionPane.NO_OPTION);//Keep asking until the user selects NO Option
}

}