# How to get a float out of an for loop?

• 07-07-2011, 05:28 PM
Lund01
How to get a float out of an for loop?
I have this code:

Code:

```                                         for (int i = 0; i < peptide_zelfInfo.length(); i++) {                                           String ss = peptide_zelfInfo.substring(i, i +1);                                           float mass = y.get(ss);                                           float massa = mass + y.get(ss);                                           System.out.println(massa);                                         }```
But how do I get the 'massa' out of this for loop?
• 07-07-2011, 05:32 PM
turk
I don't understand your question but you can't define a variable inside a loop, so you should do it like this:

Code:

```String ss = ""; float mass = 0.0; float massa = 0.0; for (int i = 0; i < peptide_zelfInfo.length(); i++) {         ss = peptide_zelfInfo.substring(i, i +1);         mass = y.get(ss);         massa = mass + y.get(ss);         System.out.println(massa); }```
• 07-07-2011, 05:40 PM
JosAH
It's not just one float value but it's one for every loop iteration. Please rethink your question and ask again ... possible answers could be a List of floats, one of the added to the List per loop iteration. At the end the entire List could be returned; but that is just guessing ...

kind regards,

Jos
• 07-07-2011, 05:42 PM
Lund01
I would like to have the 'massa' used outside the for loop.
Too print in in my XML file.
• 07-07-2011, 06:07 PM
Norm
There were two choices offered. Did either of those solve your problem?
If not explain what you want that is different from what those two solutions offered.
• 07-07-2011, 06:13 PM
Lund01
I am trying a list right now. :)
• 07-07-2011, 06:24 PM
JosAH
Quote:

Originally Posted by Lund01
I would like to have the 'massa' used outside the for loop.
Too print in in my XML file.

There isn't just one 'massa' value; there is one for each loop iteration. I don't think you have actually thought out a solution to your problem.

kind regards,

Jos
• 07-08-2011, 02:27 AM
Junky
Quote:

Originally Posted by turk
you can't define a variable inside a loop

Yes you can. In this case OP needs to declare the variable outside(before) the loop but in general there is nothing wrong with declaring variables inside loops.
• 07-08-2011, 08:43 PM
Lund01
I have tried an arrayList for this one, and it worked perfect!
Thanks everyone. :)