Let's say I have 3 textfields on a JFrame Form. I need to get the values in the textfields and sum them up. And I want to do it with a loop. I want to make my textfields' names txt1, txt2, and txt3. So would you please help me with the syntax?
Asked
Active
Viewed 1,661 times
-3
-
1Put txt1, txt2, and txt3 in an array. – Eduardo Mar 08 '12 at 21:55
-
2-1; you've been on SO long enough to know that this is not a place to have code generated for you, but one to help you with specific coding problems - at least show us what you tried so far. – G. Bach Mar 08 '12 at 21:57
-
I have tried to write variable names in a loop like that: (txt + i).getText. If the question is so easy im sorry Im new at java don't have much idea about syntax. – kmetin Mar 08 '12 at 21:58
-
4@KutluhanMetin: this has nothing to do with easy or hard, and all to do with you showing us the fruits of your labors. Why not post what you've tried as an edit to your question. How can we know what you're doing wrong and help you if we don't see your errors? And if you don't know much about syntax, [**look it up!**](http://docs.oracle.com/javase/tutorial/reallybigindex.html) – Hovercraft Full Of Eels Mar 08 '12 at 22:17
1 Answers
3
Initializing an array
textField txt[3];
for(int i=0;i<3;i++)
{
txt[i]=new textField();
}
After you've input in the textField,
double ans=0;
for(int i=0;i<3;i++)
{
ans+=double.parseDouble(txt[i].paramString());
}

YankeeWhiskey
- 1,522
- 4
- 20
- 32
-
+1 `List
` would be an alternative; there's a related example [here](http://stackoverflow.com/a/8703807/230513). – trashgod Mar 09 '12 at 01:20