Name Popularity GUI From 2006 to 2010. Prompt user for name, gender and year. Us
ID: 3807097 • Letter: N
Question
Name Popularity GUI
From 2006 to 2010.
Prompt user for name, gender and year.
Use a ComboBox for gender and year.
Use a TextBox for the name
Use a button named “Find Popularity”
Application should read text from provided files, not be hardcoded.
If there is no ranking for the name/info entered, a message “Sorry, no ranking for ……..(baby’s name) in….(year) ” should be shown in results Textbox.
Sorry, I don't know how to add text files to the question but the files have lists that look like the below:
1 Jacob 21,036 Isabella 22,222
2 Ethan 19,783 Emma 17,830
3 Michael 18,822 Olivia 17,374
4 Alexander 18,175 Sophia 16,869
There are 5 files, each pertaining to a certain year from 2006 to 2010
Example output:
What I'm really having trouble with is accessing, sorting and then getting results from the text files.
Its very similar to the program below, except the program I need uses JComboBoxes and is in GUI format.
BookmarkBookmarked
(Baby name popularity ranking) The popularity ranking of baby names from year 2006 to 2015 can be downloaded from www.ssa.gov/oact/babynames and stored in files named babynamerank2006.txt, babynamerank2007.txt, …… babynamerank2015.txt. Each file contains 1000 lines. Each line contains a ranking, a boy’s name, number for the boy’s name, a girl name and number for the girl’s name. For example, the first two lines in the file babynameranking2010.txt are as follows:
1 Jacob 21,875 Isabella 22,731
2 Ethan 17,866 Sophia 20477
So the boy’s name Jacob and girl’s name Isabella are ranked #1 and the boy’s name Ethan and the girl’s name Sophia are ranked #2. 21875 boys are names Jacob and 22,731 girls are names Isabella in the year 2010.
Write a program that enables the user to select a year, gender, and enter a name to display the ranking of the name for the selected year and gender, as shown in the following figure. Prompt the user to enter another inquiry or exit the program. To achieve the best efficiency, create two arrays for boy’s names and girl’s names, respectively. Each array has 10 elements for 10 years. Each element is a map that stores a name and its ranking in a pair with the name as the key. Here is a sample run.
Explanation / Answer
/*
* considering that the text file names are 2006.txt,2007.txt,2008.txt,2009.txt,2010.txt
* please change locations to your text files in the code,
* fileMap.put("2006", "E:/2006.txt"); ==>> say , fileMap.put("2006", "E:/2006YearNames.txt")
fileMap.put("2007", "E:/2007.txt");
fileMap.put("2008", "E:/2008.txt");
fileMap.put("2009", "E:/2009.txt");
fileMap.put("2010", "E:/2010.txt");
Hope you find your requirements in the code,
ALL THE BEST :)
*/
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main {
Map<String,String> fileMap;
JFrame f;
JPanel yearPanel;
JLabel yearLabel;
JComboBox yearBox;
JPanel genderPanel;
JLabel genderLabel;
JComboBox genderBox;
JPanel namePanel;
JLabel nameLabel;
JTextField tf1;
JPanel textPanel;
JTextField textField;
Main(){
fileMap = new HashMap<String,String>();
fileMap.put("2006", "E:/2006.txt");
fileMap.put("2007", "E:/2007.txt");
fileMap.put("2008", "E:/2008.txt");
fileMap.put("2009", "E:/2009.txt");
fileMap.put("2010", "E:/2010.txt");
String years[]={"2006","2007","2008","2009","2010"};
String genders[]={"MALE","FEMALE"};
f=new JFrame("Baby Name Popularity");
//YEAR
yearPanel= new JPanel();
yearLabel =new JLabel("Year");
yearPanel.add(yearLabel);
yearBox=new JComboBox(years);
yearPanel.add(yearBox);
yearPanel.setLayout(new GridLayout(1, 2));
f.add(yearPanel);
//GENDER
genderPanel= new JPanel();
genderLabel =new JLabel("Gender");
genderPanel.add(genderLabel);
genderBox=new JComboBox(genders);
genderPanel.add(genderBox);
genderPanel.setLayout(new GridLayout(1, 2));
f.add(genderPanel);
//NAME
namePanel= new JPanel();
nameLabel =new JLabel("NAME");
namePanel.add(nameLabel);
tf1 = new JTextField();
namePanel.add(tf1);
namePanel.setLayout(new GridLayout(1, 2));
f.add(namePanel);
//FindRanking button
JPanel buttonPanel= new JPanel();
buttonPanel.setLayout(new GridLayout(1, 2));
JButton jb =new JButton("Find Ranking");
buttonPanel.add(jb,0);
f.add(buttonPanel);
//TextField
textPanel= new JPanel();
textField = new JTextField();
textPanel.add(textField);
textPanel.setLayout(new GridLayout(1, 2));
f.add(textPanel);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String year = (String)yearBox.getSelectedItem();
String gender =(String)genderBox.getSelectedItem();
String name =(String)tf1.getText();
readFile(year);
}
});
f.setLayout(new GridLayout(5, 2,10,20));
f.setSize(400,500);
f.setVisible(true);
// UI OVER
//TODO
}
public static void main(String args[])
{
new Main();
}
public void readFile(String FILENAME){
// TODO Auto-generated constructor stub
if(fileMap.containsKey(FILENAME))
FILENAME = fileMap.get(FILENAME);
BufferedReader br = null;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
boolean found =false;
String name = (String)tf1.getText().trim();
while ((sCurrentLine = br.readLine()) != null) {
sCurrentLine=sCurrentLine.replaceAll("\s+"," ");
for(int i=0;i<sCurrentLine.split(" ").length;i++)
{
String s=sCurrentLine.split(" ")[i];
if(s.equalsIgnoreCase(name)){
found=true;
textField.setText(""+s+" was #"+sCurrentLine.split(" ")[i+1]+" in "+yearBox.getSelectedItem()+"!");
}
}
if(!found)
{
textField.setText("NAME NOT FOUND");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.