java - Accessing variables with different 'last name' -
i using netbeans create java application. created text field named jtextfield1 through jtextfield16. have store values of these text fields in 2d array, not getting how so. want access text fields loop.
how should address these variables loop?
int count=0; (int i=0; i<4; i++) { (int j=0; j<4; j++) { a[i][j] = integer.parseint(jtextfield1.gettext()); } }
instead of
jtextfield field1 = ...; jtextfield field2 = ...; ... jtextfield field16 = ...; you can use array:
jtextfield[] fields = new jtextfield[16]; fields[0] = ...; fields[1] = ...; ... fields[15] = ...; then loop becomes easy write:
a[i][j] = integer.parseint(fields[i * 4 + j].gettext()); edit
in netbeans, can use find & replace replace variables automatically. example, assuming variables called jfield1, jfield2 etc., can do:
- edit > replace in projects
- containing text:
jfield(\d+) - tick "regular expression"
- replace with:
fields[$1] - scope: select file
- find -> opens search window
- click on "replace matches"
- you need finalise decrementing indices 1 (that has manual)
et voila, less minute.
alternatively, if there few variables change, place cursor on variable name, press ctrl+r , change name: automatically change occurences of variable in project.
Comments
Post a Comment