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

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

I have this code:

Java 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?

2. Member
Join Date
Jul 2011
Posts
7
Rep Power
0
I don't understand your question but you can't define a variable inside a loop, so you should do it like this:

Java 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);
}```

3. 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

4. I would like to have the 'massa' used outside the for loop.
Too print in in my XML file.

5. 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.

6. I am trying a list right now. :)

7. 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

8. 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.

9. I have tried an arrayList for this one, and it worked perfect!
Thanks everyone. :)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•