Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of numbers inside a JTextField - Java

I'm trying to make a JTextField receive an array of numbers. I tryed with "for" and all the stuff needed to run an array, but without succes. Here is the piece of code.

private void cbxGeraValorActionPerformed(java.awt.event.ActionEvent evt) 
{        
    if(cbxRandom.isSelected())
    {
        double[] num = new double[10];

        for(int i=0; i<10; i++)
        {
            Random r = new Random();
            num[i] = r.rnd;
            txtValor.setText(String.valueOf(num[i]));
        }
    }
like image 382
fabioleardini Avatar asked Mar 01 '26 12:03

fabioleardini


2 Answers

if(cbxRandom.isSelected())
{
    double[] num = new double[10];
    String newtxt = "";
    for(int i=0; i<10; i++)
    {
        Random r = new Random();
        num[i] = r.rnd;
        newtxt += String.valueOf(num[i]);
    }
    txtValor.setText(newtxt); //do setting only after the loop ends
}
like image 104
Bhesh Gurung Avatar answered Mar 03 '26 02:03

Bhesh Gurung


if(cbxRandom.isSelected())
{
    double[] num = new double[10];
    String newtxt = "";
    for(int i=0; i<10; i++)
    {
        Random r = new Random();
        num[i] = r.rnd;
        newtxt += String.valueOf(num[i]) + " "; // Separate numbers
    }
    txtValor.setText(newtxt); //do setting only after the loop ends
}
like image 32
rendon Avatar answered Mar 03 '26 02:03

rendon