Dynamic variables

M

McGowan

Hi, I am trying to loop through 81 text boxes each time reading the
text from the box and placing it in a 2d array.

My code below shows it doing just the first line so only 9 text boxes

private void jButton1_actionPerformed(ActionEvent e) {
int userAnswer [][] = new int [9][9];

for (int i = 0; i < 9; i++) {
try {
userAnswer [0] = Integer.valueOf(A1.getText());
} catch (NumberFormatException nf) {
userAnswer[0] = 0;
}
System.out.println(i + " " + userAnswer[0]);
}
}

Where it says A1 I want it to change automatically to A2, A3, A4 and
so on on each iteration of the loop increasing by 1 all the way to 9.

Any ideas?

Thanks,
OIiver
 
H

Hendrik Maryns

McGowan schreef:
Hi, I am trying to loop through 81 text boxes each time reading the
text from the box and placing it in a 2d array.

My code below shows it doing just the first line so only 9 text boxes

private void jButton1_actionPerformed(ActionEvent e) {
int userAnswer [][] = new int [9][9];

for (int i = 0; i < 9; i++) {
try {
userAnswer [0] = Integer.valueOf(A1.getText());
} catch (NumberFormatException nf) {
userAnswer[0] = 0;
}
System.out.println(i + " " + userAnswer[0]);
}
}

Where it says A1 I want it to change automatically to A2, A3, A4 and
so on on each iteration of the loop increasing by 1 all the way to 9.

Any ideas?


Put your A* in an array of their own (A would offer itself as a name,
were it not that by convention, names of variables start with a lower
case letters, and such short names are very cryptic), and use the array
index.

H.
--
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGsLc5e+7xMGD3itQRAmeFAJsGQPV5qOogMDY493Tne7cKZK7wrgCfVJM3
nn/drtROHITgmNjgobbchSc=
=kn+J
-----END PGP SIGNATURE-----
 
D

Daniel Pitts

Hi, I am trying to loop through 81 text boxes each time reading the
text from the box and placing it in a 2d array.

My code below shows it doing just the first line so only 9 text boxes

private void jButton1_actionPerformed(ActionEvent e) {
int userAnswer [][] = new int [9][9];

for (int i = 0; i < 9; i++) {
try {
userAnswer [0] = Integer.valueOf(A1.getText());
} catch (NumberFormatException nf) {
userAnswer[0] = 0;
}
System.out.println(i + " " + userAnswer[0]);
}
}

Where it says A1 I want it to change automatically to A2, A3, A4 and
so on on each iteration of the loop increasing by 1 all the way to 9.

Any ideas?

Thanks,
OIiver


Is this by any chance a Sudoku related project :)

Like Hendrik suggests, you should use an array of textAreas, rather
than 81 seperate instances. That way you can create them and add them
to your panel in a loop as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top