0

I have this button, which opens the file directory on the computer and from where you can choose the file and execute the commands. I have each of them in a class, but only one of them is running. How can I make it execute first one class and then the other? The codes is at follows:

JButton btnFindMe = new JButton("Find theta");
btnFindMe.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        JFileChooser fctheta = new JFileChooser();
        fctheta.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        int returnVal = fctheta.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            final File filetheta = fctheta.getSelectedFile();

            //counts the thetas

            class Inputcontatheta {
                public int main (String [] arg) throws Exception{
                    BufferedReader CSVFile = new BufferedReader (new FileReader (filetheta));
                    String dataRow = CSVFile.readLine();
                    int ii;
                    ii=0;
                    while (dataRow != null && !dataRow.isEmpty()){
                        dataRow = CSVFile.readLine ();
                        ii++;
                    }
                    CSVFile.close ();
                    return ii;
                }
            }
            //  exectues gaussians
            class Inputtheta {
                public void main (String [] arg) throws Exception{
                    Inputcontatheta dimensao= new Inputcontatheta();
                    int x=dimensao.main(arg);
                    BufferedReader CSVFile = new BufferedReader (new FileReader (filetheta));
                    String dataRow = CSVFile.readLine();
                    double [][] gaussiana = new double [x][6];
                    int j;
                    j=0;
                    while (dataRow != null && !dataRow.isEmpty()){
                        String [] dataArray = dataRow.split (",");
                        double[] doubleArray =new double[dataArray.length];
                        int i=0;
                        while (i< dataArray.length ) {
                            doubleArray[i]= Double.parseDouble(dataArray[i]);
                            i++;
                        }
                        gaussiana[j][0]=1.0;
                        gaussiana[j][1]=0.25;
                        gaussiana[j][2]=doubleArray[0];
                        gaussiana[j][3]=-doubleArray[0];
                        gaussiana[j][4]=doubleArray[1];
                        gaussiana[j][5]=doubleArray[2];
                        j++;
                        System.out.println(Arrays.toString(doubleArray));
                        dataRow = CSVFile.readLine();
                    }
                    CSVFile.close ();

                    System.out.println(gaussiana[1][5]);
                    //return gaussiana [][] double;
                }}
            }
        }});
Cœur
  • 37,241
  • 25
  • 195
  • 267
user3543711
  • 1
  • 1
  • 3
  • Well, probably I can't have two classes on the same button. I need to first read the file and count it, close it, and open it again and use the input. I'm trying to put everything on the same class, but I'm not sure about the viability or if there's any other simpler or prettier way to do this – user3543711 Jun 01 '14 at 19:11

1 Answers1

0

Try transforming your Inputcontatheta and Inputtheta classes into methods of the ActionListener like so:

btnFindMe.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        JFileChooser fctheta = new JFileChooser();
        fctheta.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        int returnVal = fctheta.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File fileTheta = fctheta.getSelectedFile();
            int x = countThetas(fileTheta);
            executeGaussians(fileTheta, x);
        }
    }

    private int countThetas(File fileTheta) {
        // stuff from Inputcontatheta here
    }

    private void executeGaussians(File fileTheta, int x) {
        // stuff from Inputtheta here
    }
});

That being said, maybe instead of just counting the number of rows in your csv file the countThetas method could actually parse the csv file into a double[][] array and you could pass that array to executeGaussians. (You can use java.util.ArrayList if you don't know the length of your data beforehand.)

PoByBolek
  • 3,775
  • 3
  • 21
  • 22