I have tried many things obviously not everything or it would work, but i need your help. i have run out of ideas on how to fix this. im programming a fishing database where users enter the data and over time it collects and shows them were the type of fish are at certain locations at certain months in the year. but the Search feature telling them there information isnt working. please help here is my code( please note this is my first time using this):

void createSearchTab ()
    {
        fishListSearch = createJList (fishArray);
        locsListSearch = createJList (locationArray);
        whenListSearch = new JList (monthArray);

        westPanel.setLayout (new GridLayout (3, 2));
        westPanel.add (new JLabel ("What fish do you want to Catch?"));
        westPanel.add (new JScrollPane (fishListSearch));
        westPanel.add (new JLabel ("Where did you catch it?"));
        westPanel.add (new JScrollPane (locsListSearch));
        westPanel.add (new JLabel ("What month is it?"));
        westPanel.add (new JScrollPane (whenListSearch));

        southPanel.setLayout (new FlowLayout ());
        southPanel.add (searchData);
        searchTab.add (titleOne, BorderLayout.PAGE_START);
        searchTab.add (westPanel, BorderLayout.CENTER);
        searchTab.add (southPanel, BorderLayout.SOUTH);
        searchResults = new JPanel (new BorderLayout ());
        searchResults.add (resultExit, BorderLayout.SOUTH);
        resultExit.addActionListener (new ActionListener ()
        {
            public void actionPerformed (ActionEvent arg0)
            {
                tabbs.remove (searchResults);
            }
        }
        );
        searchData.addActionListener (new ActionListener ()
        {
            public void actionPerformed (ActionEvent arg0)
            {
                fish = fishArray.get (fishListSearch.getSelectedIndex ()).toString ();
                month = monthArray [whenListSearch.getSelectedIndex ()];
                location = locationArray.get (locsListSearch.getSelectionMode ()).toString ();
                tabbs.addTab ("Search Results", searchResults);
                while (catches.get (i) != null)
                {
                    i = i + 1;
                    while (catches.get (i) != null)
                    {
                        i = i + 1;
                        while (catches.get (i) != null)
                        {
                            if (catches.get (i) == month)
                            {
                                if (catches.get (i - 1) == location)
                                {
                                    if (catches.get (i - 2) == fish)
                                        resultLabel.setText ("During the Month of " + catches.get (i) + " at " + catches.get (i - 1) + ", " + catches.get (i + 1) + " " + catches.get (i - 2) + " were caught.");
                                    else
                                        resultLabel.setText (catches.get (i + 1) + " " + catches.get (i - 2) + " were caught at " + catches.get (i + 1) + ".");
                                }
                                else
                                {
                                    if (catches.get (i - 2) == fish)
                                        resultLabel.setText ("During the Month of " + catches.get (i) + ", " + catches.get (i + 1) + " " + catches.get (i - 2) + " were caught.");
                                    else
                                        resultLabel.setText (catches.get (i + 1) + " " + catches.get (i - 2) + " were caught total.");
                                }
                            }
                            else
                            {
                                if (catches.get (i - 1) == location)
                                {
                                    if (catches.get (i - 2) == fish)
                                        resultLabel.setText ("During the Month of " + catches.get (i) + " at " + catches.get (i - 1) + ", " + catches.get (i + 1) + " fish were caught.");
                                    else
                                        resultLabel.setText (catches.get (i + 1) + " fish were caught at " + catches.get (i + 1) + ".");
                                }
                                else
                                {
                                    if (catches.get (i - 2) == fish)
                                        resultLabel.setText ("During the Month of " + catches.get (i) + ", " + catches.get (i + 1) + " fish " + " were caught.");
                                    else
                                        resultLabel.setText (catches.get (i + 1) + " fish were caught total.");
                                }
                            }
                            searchResults.add (resultLabel, BorderLayout.CENTER);
                            if (catches.get (i) == null)
                                break;
                        }
                        if (catches.get (i) == null)
                            break;
                    }
                    if (catches.get (i) == null)
                        break;
                }
            }
        }
        );
    }
    public static void readCaught () throws IOException
    {
        String line;
        BufferedReader input;
        input = new BufferedReader (new FileReader ("caught.txt"));
        line = input.readLine ();
        while (line != null)
        {
            int i = 0;
            String fields[] = line.split (",");
            CatchRecord caught = new CatchRecord ();
            caught.fish = fields [0];
            caught.location = fields [1];
            caught.month = fields [3];
            caught.count = Integer.parseInt (fields [2]);
            catches.add (i, caught);
            line = input.readLine ();
            ++i;
        }
    }


    public static void writeCaught () throws IOException
    {
        PrintWriter output;
        int randomCount = 0;
        output = new PrintWriter (new FileWriter ("caught.txt", true));
        output.print (fish + "," + location + "," + month.length () + "," + count + "\n");
        output.close ();
    }

Recommended Answers

All 2 Replies

State the exact error you are getting, or the part of the code that isn't working according to plan.

void createSearchTab ()
    {
        ...
    public static void readCaught () throws IOException
    {
        String line;
        BufferedReader input;
        input = new BufferedReader (new FileReader ("caught.txt"));
        line = input.readLine ();
        while (line != null)
        {
            int i = 0;
            String fields[] = line.split (",");
            CatchRecord caught = new CatchRecord ();
            caught.fish = fields [0];
            caught.location = fields [1];
            caught.month = fields [3];
            caught.count = Integer.parseInt (fields [2]);
            catches.add (i, caught);
            line = input.readLine ();
            ++i;
        }
    }
...

Just quickly looking over the code I think a problem you are encountering would be in this method. At the start of the while loop you make i = 0. At the you then read some info from the file and store it into catches. then increment i (which does not need to be done as ++i). Providing you have more info in the file you start your loop again (which will cause the problem as i is set back to 0).

To summarize you are only really adding one entry (or to your collection that all it sees) :)

Like mentioned above please post exactly the problem you are having and we can help out more.

commented: You picked it up well +2
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.