|
|
Re: arrays
|
Posted: Apr 13, 2005 10:49 PM
|
|
This is, because you overwrite the name everytime the input field is showed, because name is just a simple String.
You need to store ALL the names, therefore you need some sort of array or a Vector.
Since you have a fix number of inputs, an Array is what you need.
Hint: At the moment counter goes from 1 to 5. It is better to let it go from 0 to 4, since all Java indizes start at 0. Hint: Allways let the names of variables and packages begin with a small letter and the names of Classes with a capital letter, that makes them more distinguishable.
Hint: Your while loop works, but it would look better if you would use a for-to loop. That's what I will do for the output. Note that the index variable of a for-to loop is not visible outside the loop(that's good, so you can reuse the same name).
Hint: The setText() Method for writing the data of the output won't work, because it does not append text, but overwrite the content of the Area. that's why I use the apend method.
So:
final int nNames = 5; //just to make it easier to edit later
String[] name = new String[nNames];
String doB = new String[nNames];
int counter = 0;
do{
name[counter] = JOptionPane.showInputDialog(" Enter Name ");
doB[counter] = JOptionPane.showInputDialog(" Enter Date of Birth ");
} while (counter < nNames);
.
.
.
outputArea.setText( "Name\tDate Of Birth" + "\n\n" + counter + "\t" ); //don't know what that should do, but anyway
for (int i = 0; i < nNames; i++) { //that does exactly the same as your while loop above, but i is not visible after the loop, wich usually is what you want.
outputArea.append(...); //name[i] and doB[i]
}
|
|