简体   繁体   中英

Input info 2x2 Array

Im trying to input data on a 2x2 array, i cant use a "for" because the use of buttons, the first and second place are ok but something is wrong when i go to the next dimension of the array (the j, i guess) i would appreciate some help, thx :)

    public void actionPerformed (ActionEvent e){
            if (e.getSource() == btingreso){
                if (i<c1.length)
                    if (j<c1[i].length){
                        c1[i][j]= new compu_partes (txtnombre.getText(),Integer.parseInt(txtcantidad.getText()),txtcodigo.getText(),Double.parseDouble(txtprecio.getText()));
                            i++;
                    }
                    j++;
                    i=0;
            }
}

Be careful with your indentation! It suggests that the 2nd if -block also includes j++; and i=0; , which these two statements are not .

Also I think you incremented your indices slighly wrong.

This should work:

public void actionPerformed (ActionEvent e){
    if (e.getSource() == btingreso){
        if (i<c1.length){
            if (j < c1[i].length){
                c1[i][j] = new compu_partes(...);
                j++;
            }
            if (j == c1[i].length){
                i++;
                j=0;
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM