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...
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...
I have run huge piece of code. I need a UML for it. I dont what...
I have run huge piece of code. I need a UML for it. I dont what a UML is, but my professor said he needs it. Please help. Thanks. import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.Scanner; public class CreditCardValidation { private String inputFileName; private String outputValidCardFileName; private String outputInvalidCardFileName; public static void main(String[] args) { CreditCardValidation ccValidationObj = new CreditCardValidation(); ccValidationObj.readFile(); } //Deafult Constrictor to inilialize instance variable public CreditCardValidation () { this.inputFileName = "data.txt"; this.outputValidCardFileName =...
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.util.*; public class SJF { public static...
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.text.ParseException; import java.util.*; public class SJF { public static void readFromFile() throws IOException { BufferedReader bufReader = new BufferedReader(new FileReader("processes.txt")); ArrayList<String> listOfLines = new ArrayList<>(); String line = bufReader.readLine(); while (line != null) { listOfLines.add(line); line = bufReader.readLine(); } bufReader.close(); System.out.println("Content of ArrayLiList:"); // split by new line int num = 0; for (String line1 : listOfLines) { String line2[] = line1.split(","); // int burstTime = Integer.parseInt(line2[3].trim()); // String retrival = listOfLines.get(0); System.out.println("...
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...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT