Question
Java Program Help Reading Text Files In Folder I'm trying to write a program that prompts the user to choose a year and gender (both
Java Program Help Reading Text Files In Folder
I'm trying to write a program that prompts the user to choose a year and gender (both JComboBox) and then enter a name. The program should give the user the "ranking" or "popularity" of the name entered. I can't seem to get my program to read the documents in the folder.
The folder is named "Years" and contains 6 Word Documents (2006, 2007, 2008, 2009, 2010, 2011)
Here's an example of what the documents look like when opened.
58 Owen 8,169 Kimberly 4,999
59 Lucas 8,125 Andrea 4,995
60 Charles 7,999 Megan 4,936
61 Juan 7,925 Katelyn 4,876
62 Luis 7,918 Faith 4,844
63 Adrian 7,373 Gabrielle 4,833
64 Julian 6,787 Trinity 4,828
65 Adam 6,775 Evelyn 4,745
66 Bryan 6,684 Kylie 4,711
67 Alex 6,662 Brooklyn 4,699
68 Sean 6,629 Audrey 4,645
69 Nathaniel 6,606 Leah 4,639
****Code so far***
import java.io.FileReader; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.IOException; import java.io.BufferedReader;
public class FindPopularity extends JFrame implements ActionListener {
int popular[] = new int[10]; int slno[] = new int[10]; int c = 0; String babyName [] = new String[10]; JLabel Year, Gender, Name; JTextField nam, results; JComboBox cbYear, cbGender; JButton btnFind; JFrame Window; JPanel p1, p2, p3, p4, p5;
FindPopularity() {
String []year = {"2006", "2007", "2008", "2009", "2010"}; String []gender = {"Male", "Female"}; Year = new JLabel("Select Year"); Gender = new JLabel("Gender"); Name = new JLabel("Enter name"); JComboBox
btnFind.addActionListener(this); Window = new JFrame("Baby Name Popularity"); Window.setLayout(new GridLayout(5,2));
p1 = new JPanel(new FlowLayout()); p2 = new JPanel(new FlowLayout()); p3 = new JPanel(new FlowLayout()); p4 = new JPanel(new FlowLayout()); p5 = new JPanel(new FlowLayout());
p1.add(Year); p1.add(cbYear); p2.add(Gender); p2.add(cbGender); p3.add(Name); p3.add(nam); p4.add(btnFind); p5.add(results);
Window.add(p1); Window.add(p2); Window.add(p3); Window.add(p4); Window.add(p5);
Window.setVisible(true); Window.setSize(400,400); Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
int search(String data) { int pos = 0;
for(int x = 0; x < c-1; x++)
if((data.compareTo(babyName[x])) == 0) {
pos = x; break; }
return pos; }
public void actionPerformed(ActionEvent ae) { if(ae.getSource() == btnFind) { String myresults = ""; int index = search(nam.getText());
if(index == 0) myresults = "No such baby name available!";
else myresults = myresults + babyName[index] + " was #" + slno[index] + " in " + cbYear.getSelectedItem() + "!"; results.setText(myresults); } }
void readFile() {
try {
FileReader reader = new FileReader("/Users/Desktop/Years"); BufferedReader br = new BufferedReader(reader); br = new BufferedReader(new FileReader("/Users/Desktop/Years")); String data[] = new String[10]; String data1[] = new String[3]; System.out.println("Popularity Information");
while ((data[c++] = br.readLine()) != null)
for(int x = 0; x < c; x++) {
data1 = data[x].split(" ");
slno[x] = Integer.parseInt(data1[0]); babyName[x] = data1[1]; popular[x] = Integer.parseInt(data1[2]); }
reader.close(); br.close(); }
catch (IOException e) { e.printStackTrace(); } }
public static void main(String ss[]) {
FindPopularity fp = new FindPopularity(); fp.readFile(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started