Results 1 to 9 of 9
  1. #1
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default 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. #2
    turk is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    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. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    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
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

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

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    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. #6
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

    I am trying a list right now. :)

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Lund01 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by turk View Post
    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. #9
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

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

Similar Threads

  1. FloatBuffer and 'put( float[] src )'
    By maxkiva in forum New To Java
    Replies: 4
    Last Post: 08-08-2010, 11:56 AM
  2. Error in float value
    By JMaste in forum New To Java
    Replies: 5
    Last Post: 07-11-2010, 04:08 PM
  3. string to float
    By Sungron in forum New To Java
    Replies: 3
    Last Post: 02-02-2010, 09:23 AM
  4. Float vs. Double
    By javanub in forum New To Java
    Replies: 1
    Last Post: 11-23-2008, 12:11 PM
  5. Class float
    By Peter in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:17 AM

Posting Permissions

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