Question

I cannot for the life of me get this program to run properly. I don't know...

I cannot for the life of me get this program to run properly. I don't know what I'm doing wrong. Could the format of my text files be the issue?

Edit: The program works this way, you choose a year and a gender and then enter a name. When you press the button its should give you the ranking or popularity of the name. There are 5 files that I have to search through, named 2006.txt to 2010.txt.

First the files looked like this:

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

And then I changed them to this. Neither one works.

1   Jacob   1   Emily
2   Michael   2   Emma
3   Joshua   3   Madison
4   Ethan   4   Isabella

Code:

import java.io.File;
import java.io.FileReader;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Locale;

public class FindPopularity extends JFrame implements ActionListener
{

    int popular[] = new int[12];
    ArrayList popular = new ArrayList<>();
    ArrayList slno = new ArrayList<>();
    ArrayList babyName = new ArrayList<>();

    int c = 0;

    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");
        cbYear = new JComboBox(year);
        JComboBox cbGender = new JComboBox<>(gender);
        nam = new JTextField(20);
        results = new JTextField(20);
        btnFind = new JButton("Find Rank");
        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 = -1;
        for(int x = 0; x < c-1; x++)
            if((data.compareTo(babyName.get(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 == -1)
                myresults = "No such baby name available!";
            else
                myresults = myresults + babyName.get(index) + " was #" + slno.get(index) + " in " + cbYear.getSelectedItem() + "!";
            results.setText(myresults);
        }
    }
    void readFile()
    {
        try
        {
         
            File file = new File("/Users/Marque/Desktop/2006.txt");
            FileReader reader = new FileReader(file);

       
            BufferedReader br = new BufferedReader(reader);

            String data[] = new String[20];
            String data1[] = new String[3];
            System.out.println("Popularity Information");

            while ((data[c] = br.readLine()) != null)
            
                {
                    System.out.println(data[c]);
                  
                    data1 = data[c].trim().split("\\s+");;

                    slno.add(c,Integer.parseInt(data1[0]));

               
                    babyName.add(c,data1[1].trim());
                    NumberFormat nf = NumberFormat.getInstance(Locale.US);
                    popular.add(c,nf.parse(data1[2].trim()).intValue());
                    c++;
                }
            reader.close();
            br.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    public static void main(String ss[])
    {
        FindPopularity fp = new FindPopularity();
        fp.readFile();
    }
}

Homework Answers

Answer #1

It worked for me

Keep the data : Seperated by one space
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

=====
See Output






Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Hi, I am trying to create an XML JTree viewer using the DOM parser instead of...
Hi, I am trying to create an XML JTree viewer using the DOM parser instead of the SAX parser, I am having trouble changing my code in order to utilize the DOM parser to extract the XML data into the tree structure. Would you be able to explain what changes should be made to the code in order to use the DOM parser instead of the SAX parser? // Java Packages //      import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
Create the ArrayList program example in listing 13.1, Battlepoint. Describe the code (in your WORD document)...
Create the ArrayList program example in listing 13.1, Battlepoint. Describe the code (in your WORD document) in the 'if' statement if (targets.indexOf(shot) > -1) { ADD a NEW ArrayList of points called 'misses' ADD code to add a point to the 'misses' list on a miss (if a SHOT does NOT hit a TARGET) ADD code to place an 'M' in the final output map for all shots that were MISSES. ADD code to place an H on the target...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list...
Assignment 1 - ITSC 2214 I have to complete the array list for a shopping list code and can't figure out how to complete it: Below is the code Complete the three empty methods (remove(), find(), and contains()) in the ShoppingListArrayList.java file. These methods are already implemented in the ShoppingListArray class. /////////////////////////////////////////////////////////////////////////////////////////////////////////// Grocery Class (If this helps) package Shopping; public class Grocery implements Comparable<Grocery> { private String name; private String category; private int aisle; private float price; private int quantity;...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...