Tryin to sort a search method for abit of code. Have an array list reading from a csv. Any help on how I can get a search method searching through and displaying onto a jTextArea? This is my code in my Gui

public class DataGUI extends javax.swing.JFrame {

public class DataGUI extends javax.swing.JFrame {

    /**
     * Creates new form DataGUI
     */
    ArrayList<Bmx> people;
    public int count = 0;
    DefaultTableModel tableModel = new DefaultTableModel(
            new Object[][]{}, new String[]{
        "Rank", "First Name", "Last Name", "Uni Code", "Country", "Time"
    });

    public DataGUI(ArrayList<Bmx> people) {
        initComponents();
        this.people = people;
        drawTable();
        jTable1.setModel(tableModel);
        jTable1.setAutoCreateRowSorter(true);


    }

    public void drawTable() {
        tableModel.setRowCount(0);
        for (int i = 0; i < people.size(); i++) {
            Object[] object = new Object[9];
            object[0] = people.get(i).getRank();
            object[1] = people.get(i).getFirstName();
            object[2] = people.get(i).getLastName();
            object[3] = people.get(i).getUciCode();
            object[4] = people.get(i).getCountry();
            object[5] = people.get(i).getTime();
            tableModel.addRow(object);

        }
    }

    public void dataShow() {

        jTextField1.setText("" + people.get(count).getRank());
        jTextField2.setText("" + people.get(count).getFirstName());
        jTextField3.setText("" + people.get(count).getLastName());
        jTextField5.setText("" + people.get(count).getUciCode());
        jTextField4.setText("" + people.get(count).getCountry());
        jTextField6.setText("" + people.get(count).getTime());

        count++;
    }

    public void dataMinus() {
        jTextField1.setText("" + people.get(count).getRank());
        jTextField2.setText("" + people.get(count).getFirstName());
        jTextField3.setText("" + people.get(count).getLastName());
        jTextField5.setText("" + people.get(count).getUciCode());
        jTextField4.setText("" + people.get(count).getCountry());
        jTextField6.setText("" + people.get(count).getTime());

        count--;
    }

    private void search() {
        String Name = jTextArea1.getText();
        String record = "";
        int count = 0;

        for (int i = 0; i < people.size(); i++) {
            String name = people.get(i).getFirstName();

            String[] splitName = name.split(" ");

            if (Name.equals(splitName[0]) || Name.equals(splitName[1])) {
                count++;
                record = record + "\n" + people.get(i).toString();

            }
            jTextArea1.setText("");
            jTextArea1.append(count + " result(s) found for " + Name);
            jTextArea1.append("\n " + record);
        }
    }

Recommended Answers

All 8 Replies

Anything would be greatly appriciated!

what exactly is it supposed to do, and where are you stuck? you're not giving us much information to go on here...

I have a gui and I need to create a search method that can search through the data that I have in the csv file

yes, you have a GUI.. uhm .. it has to "search" .. based on what?

according to these two methods:

public void dataShow() {
        jTextField1.setText("" + people.get(count).getRank());
        jTextField2.setText("" + people.get(count).getFirstName());
        jTextField3.setText("" + people.get(count).getLastName());
        jTextField5.setText("" + people.get(count).getUciCode());
        jTextField4.setText("" + people.get(count).getCountry());
        jTextField6.setText("" + people.get(count).getTime());
        count++;
    }
    public void dataMinus() {
        jTextField1.setText("" + people.get(count).getRank());
        jTextField2.setText("" + people.get(count).getFirstName());
        jTextField3.setText("" + people.get(count).getLastName());
        jTextField5.setText("" + people.get(count).getUciCode());
        jTextField4.setText("" + people.get(count).getCountry());
        jTextField6.setText("" + people.get(count).getTime());
        count--;
    }

my guess is, you have 'columns' for rank, firstname, lastname, ucicode, country and time.
am I right in thinking you 'll enter one value and be searching based on that?

also: avoid having repeating code. the above methods can be simplified in two ways (that are very easy to implement), yet make your code a lot easier to read.

    public void dataShow() {
        setTexts();
        count++;
    }
    public void dataMinus() {
        setTexts();
        count--;
    }

    private void setTexts(){
        jTextField1.setText("" + people.get(count).getRank());
        jTextField2.setText("" + people.get(count).getFirstName());
        jTextField3.setText("" + people.get(count).getLastName());
        jTextField5.setText("" + people.get(count).getUciCode());
        jTextField4.setText("" + people.get(count).getCountry());
        jTextField6.setText("" + people.get(count).getTime());
    }

or:

public void setTexts(boolean add){
        jTextField1.setText("" + people.get(count).getRank());
        jTextField2.setText("" + people.get(count).getFirstName());
        jTextField3.setText("" + people.get(count).getLastName());
        jTextField5.setText("" + people.get(count).getUciCode());
        jTextField4.setText("" + people.get(count).getCountry());
        jTextField6.setText("" + people.get(count).getTime());
        if ( add )
            count++;
        else
            count--;
    }

read the contents of the file into a List or Set, and search through that, it'll take less time then each time reading through the file.

Yeah okay thanks. I just don't understand how to get it all into a button click and then displaying into a Jtextarea or something like that. I'm finding it difficult to bring it in from the array?

how to get all what in a button click? it's just writing the code in another location, you don't have to change anything to the code.

I want to have a button called 'search' and type someones name in and click and search for them. Then displaying the names in a text area. This is my data that im reading in.

1 John Franklin AUS19901024 Australia 0.57
2 Robert Kyle FRA19890316 France 0.58
3 Michael Anderson USA19930429 United States 0.58
4 William Gates NED19931209 Netherlands 0.58
5 David Jones COL19911010 Columbia 1.02
6 Richard Nicolson AUS19910625 Australia 1.03
7 Joseph Bacon USA19910117 United States 1.04
8 Charles Robinson FRA19860729 France 1.04
9 Thomas Hiel USA19850730 United States 1.04
10 Christopher Todd NZL19880710 New Zealand 1.04
11 Daniel Franklin AUS19930212 Australia 1.04
12 Matthew Kyle ARG19810102 Argentina 1.04
13 Donald Anderson CZE19840830 Czech Republic 1.04
14 Anthony Gates USA19890208 United States 1.04
15 Paul Jones LTU19830224 Lithuania 1.057
16 Mark Nicolson FRA19900625 France 1.057
17 George Bacon CZE19890427 Czech Republic 1.057
18 Steven Robinson CZE19830904 Czech Republic 1.057
19 Kenneth Hiel THA19900624 Thailand 1.057
20 Andrew Todd VEN19910615 Venezuela 1.057
21 Edward Franklin AUS19901024 Australia 1.057
22 Brian Kyle FRA19890316 France 1.057
23 Joshua Anderson USA19930429 United States 1.057
24 Kevin Gates NED19931209 Netherlands 1.057
25 Ronald Jones COL19911010 Columbia 1.057
26 Timothy Nicolson AUS19910625 Australia 1.057
27 Jason Bacon USA19910117 United States 1.057
28 Jeffrey Robinson FRA19860729 France 1.057
29 Gary Hiel USA19850730 United States 1.057
30 Ryan Todd NZL19880710 New Zealand 1.057
31 Nicholas Franklin AUS19930212 Australia 1.057
32 Eric Kyle ARG19810102 Argentina 1.057
33 Stephen Anderson CZE19840830 Czech Republic 1.24
34 Jacob Gates USA19890208 United States 1.24
35 Larry Jones LTU19830224 Lithuania 1.24
36 Frank Nicolson FRA19900625 France 1.24
37 Jonathan Bacon CZE19890427 Czech Republic 1.24
38 Scott Robinson CZE19830904 Czech Republic 1.24
39 Justin Hiel THA19900624 Thailand 1.24
40 Raymond Todd VEN19910615 Venezuela 1.24
41 Brandon Franklin AUS19901024 Australia 1.24
42 Gregory Kyle FRA19890316 France 1.24
43 Samuel Anderson USA19930429 United States 1.24
44 Patrick Gates NED19931209 Netherlands 1.24
45 Benjamin Jones COL19911010 Columbia 1.24
46 Jack Nicolson AUS19910625 Australia 1.24
47 Dennis Bacon USA19910117 United States 1.24
48 Jerry Robinson FRA19860729 France 1.24
49 Alexander Hiel USA19850730 United States 1.24
50 Tyler Todd NZL19880710 New Zealand 1.24
51 Douglas Franklin AUS19930212 Australia 1.45
52 Henry Kyle ARG19810102 Argentina 1.45
53 Peter Anderson CZE19840830 Czech Republic 1.45
54 Walter Gates USA19890208 United States 1.456
55 Aaron Jones LTU19830224 Lithuania 1.456
56 Jose Nicolson FRA19900625 France 1.456
57 Adam Bacon CZE19890427 Czech Republic 1.456
58 Harold Robinson CZE19830904 Czech Republic 1.456
59 Zachary Hiel THA19900624 Thailand 1.456
60 Nathan Todd VEN19910615 Venezuela 1.456
61 Carl Franklin AUS19901024 Australia 1.456
62 Kyle Kyle FRA19890316 France 1.456
63 Arthur Anderson USA19930429 United States 1.456
64 Gerald Gates NED19931209 Netherlands 1.456
65 Lawrence Jones COL19911010 Columbia 1.456
66 Roger Nicolson AUS19910625 Australia 1.456
67 Albert Bacon USA19910117 United States 1.456
68 Keith Robinson FRA19860729 France 1.456
69 Jeremy Hiel USA19850730 United States 1.5
70 Terry Todd NZL19880710 New Zealand 1.5
71 Joe Franklin AUS19930212 Australia 1.5
72 Sean Kyle ARG19810102 Argentina 1.5
73 Willie Anderson CZE19840830 Czech Republic 1.5
74 Jesse Gates USA19890208 United States 1.5
75 Ralph Jones LTU19830224 Lithuania 1.5
76 Billy Nicolson FRA19900625 France 1.5
77 Austin Bacon CZE19890427 Czech Republic 1.5
78 Bruce Robinson CZE19830904 Czech Republic 1.5
79 Christian Hiel THA19900624 Thailand 1.56
80 Roy Todd VEN19910615 Venezuela 1.56
81 Bryan Franklin AUS19901024 Australia 1.56
82 Eugene Kyle FRA19890316 France 1.56
83 Louis Anderson USA19930429 United States 1.56
84 Harry Gates NED19931209 Netherlands 1.56
85 Wayne Jones COL19911010 Columbia 1.56
86 Ethan Nicolson AUS19910625 Australia 1.56
87 Jordan Bacon USA19910117 United States 1.56
88 Russell Robinson FRA19860729 France 1.56
89 Alan Hiel USA19850730 United States 1.56
90 Philip Todd NZL19880710 New Zealand 1.56
91 Randy Franklin AUS19930212 Australia 1.56
92 Juan Kyle ARG19810102 Argentina 1.56
93 Howard Anderson CZE19840830 Czech Republic 1.61
94 Vincent Gates USA19890208 United States 1.61
95 Bobby Jones LTU19830224 Lithuania 1.61
96 Dylan Nicolson FRA19900625 France 1.61
97 Johnny Bacon CZE19890427 Czech Republic 1.61
98 Phillip Robinson CZE19830904 Czech Republic 1.61
99 Craig Hiel THA19900624 Thailand 1.61
100 James Todd VEN19910615 Venezuela 1.61
101 Gary Franklin AUS19901024 Australia 1.61
102 Frank Kyle FRA19890316 France 1.61
103 Daniel Anderson USA19930429 United States 1.61
104 Samuel Gates NED19931209 Netherlands 1.61
105 Aaron Jones COL19911010 Columbia 1.61
106 Sean Nicolson AUS19910625 Australia 1.61
107 Terry Bacon USA19910117 United States 1.61
108 Jeremy Robinson FRA19860729 France 1.61
109 Nathan Hiel USA19850730 United States 1.61
110 Eric Todd NZL19880710 New Zealand 1.61
111 Frank Franklin AUS19930212 Australia 1.61
112 George Kyle ARG19810102 Argentina 1.61
113 Harry Anderson CZE19840830 Czech Republic 1.61
114 James Gates USA19890208 United States 1.61
115 Harold Jones LTU19830224 Lithuania 1.61
116 Gary Nicolson FRA19900625 France 1.76
117 Sean Bacon CZE19890427 Czech Republic 1.76
118 Daniel Robinson CZE19830904 Czech Republic 1.76
119 Frank Hiel THA19900624 Thailand 1.76
120 Gerald Todd VEN19910615 Venezuela 1.76

well, you can (for instance) create a class "Entry" which keeps all that information.

List<Entry> myEntries = readFromFile();

// on clicking:
String searchName = jTextField1.getText();
List<Entry> found = new ArrayList<Entry>();
for ( Entry test : myEntries ){
  if ( test.getName().equals(searchName))
    found.add(test);
}

or, you could use the contains instead of the equals method, depending on your needs.

commented: This is a good Java solution +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.