I have this ArrayList of data filled with numbers from a file I open which I would like to be able to generate random numbers from it by entering the size of the sample and the number of samples I want. So I have the following...
private JTextField jtfN = new JTextField();// where i enter the amount of samples
private JTextField jtfn = new JTextField();// where i enter the size of samples
private ArrayList< Double> data = new ArrayList< Double>();
I have two text fields one named jtfn(size of sample) and one jtfN(amount of samples) which are where I enter the values. Then from that i have a button named jbtnGenerate whch when i click I want it to generate random numbers from data with the jtfn and jtfN entered above then putting it in a TextArea named jta
That's where I open files and add the numbers into data
if( jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION ) {
String file = jfc.getSelectedFile().getPath();
String line = null;
String[] ch;
try {
FileReader fr = new FileReader( file);
BufferedReader br = new BufferedReader(fr);
data.clear();
while( (ligne=br.readLine())!=null ) {
ch = line.split( ";" );
for (int i = 0; i < ch.length; i++) {
data.add( Double.parseDouble(ch[i]) );
}
}
br.close();
}
catch( IOException ioe ) {
}
}
That's my listener for the button "generate" where I would like for the actions to happen.
jbtnGenerer.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae ) {
try {
int sampSize = Integer.parseInt(jtfn.getText());
int nSamples = Integer.parseInt(jtfN.getText());
double samps[][] = new double[nSamples][sampSize];
for(int i = 0 ; i < nSamples; i++){
for(int j = 0 ; j < sampSize; j++)
samps[i][j] = (Double) data.toArray()[ rng.nextInt() % data.size() ];
}
jta.append(samps.toString());
} catch (NumberFormatException e) {
}
}
});
Thanks for the help