0

I am trying to parse a file for an instance of a word in Java. The word is input from a JTextField. Depending on which selection is made in a JComboBox the filepath may change. The Text file contains names and their popularity. The desired output is how that name ranked for the year that is selected. I'm struggling with setting up an appropriate search. I have been able to get the file to open and output some of the contents of the file to the Java Console, but the file contains 1000 lines and I notice that the output in the console only begins with entry number 950 or so.

Here is what I have so far:

 public void actionPerformed(ActionEvent e)
    {
        //Parse Input Fields
        String name = inputArea.getText();
        String genderSelected = genderList.getSelectedItem().toString();
        String yearSelected = yearList.getSelectedItem().toString();

        String yearFile = "Babynamesranking"+yearSelected+".txt";


        try
        {
            File inputFile = new File(yearFile);
            BufferedReader in = new BufferedReader(new FileReader(inputFile));

/**This is where I draw a blank and need help*/

            in.close();
        }
        catch (IOException exception)
        {
            JOptionPane.showMessageDialog(null, "Error Reading File");
        }

I have more work to do after I get this part working to display the results in the GUI and such, but that is easy and I can do that. Just struggling with how to get this part to work.

Example of text file we are parsing:

1   Jacob   22,507  Emma    18,765
2   Michael 20,524  Isabella    18,564
3   Ethan   20,174  Emily   17,397
4   Joshua  19,133  Olivia  17,030
5   Daniel  18,935  Ava     17,007
6   Alexander   18,639  Madison     16,985
7   Anthony 18,347  Sophia  16,042
8   William 18,337  Abigail     15,045
9   Christopher 17,876  Elizabeth   11,956
10  Matthew 17,526  Chloe   11,802
11  Jayden  17,088  Samantha    11,157
12  Andrew  16,711  Addison     10,742
13  Joseph  16,483  Natalie     10,179
14  David   16,246  Mia     10,156
15  Noah    15,748  Alexis  9,685
Justiciar
  • 356
  • 1
  • 3
  • 20
  • Why not simply do `yearFile = "Babynamesranking" + yearSelected + ".txt";`? After testing that yearSelected is not null. Why make things so difficult? – DontKnowMuchBut Getting Better Mar 20 '17 at 20:57
  • @DontKnowMuchButGettingBetter That's a better solution for that part which I hadn't considered. Does not help with the primary question I am asking though, which is how to search the file for a specfic name, and then output which rank that name achieved during that year. – Justiciar Mar 20 '17 at 21:00
  • 1
    But yes, what @Frakcool states is true. When you use `==` you are comparing variables for reference equality, that the two variables refer to the exact same String object which is not what you want. The `equals(...)` method compares variables for *functional* equality, that the two Strings have the same letters in the same order in the same case, and that's what you want. – DontKnowMuchBut Getting Better Mar 20 '17 at 21:01
  • I will edit the OP to reflect your suggestion – Justiciar Mar 20 '17 at 21:05

0 Answers0