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();
}
}
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
Get Answers For Free
Most questions answered within 1 hours.