Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Return from incrementing for loop?

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Return from incrementing for loop?

    I'm trying to return a variable form a for loop that is incrementing
    here is my for loop i need to return the sum value (i know i need to change void from the method name)
    	public static void loopSum(int n){
    		int sum = 0;
            for (int i = 1; i <= n; i++) {
               sum = sum + i;


    i need to return the value of 5050 ("n" is set to 100 in the rest of the code, i just can't seem to figure this out.
    I hope i posted this in the right place. Thanks in advance!
    Last edited by helloworld922; October 3rd, 2010 at 07:47 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Return from incrementing for loop?

    The method you are in does NOT return anything. It is defined as void. If you change it to return an int, then you should be able to return a value:
    return 5050;

    Where does the value 5050 come from?

    You need to post all of the code that you are talking about.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Return from incrementing for loop HELP?!

    public class IntSumReturnValues {
     
    	public static void main(String[] args) {
            int n = 100;
           // int sumLoop= 
    		System.out.println("The sum of the numbers from 1 to "
    				+ n + " is " + loopSum(n) + ". Calculated with a loop"); // print out the "n" value and the loopSum value of 5050
     
    		System.out.println("The sum of the numbers from 1 to "
    				+ n + " is " + formulaSum(n) + ".  Calculated with the formula."); // print out the "n" value and the formulaSum value of 5050
     
     
        }
    	public static int loopSum(int n){
    		int sum = 0;
            for (int i = 1; i <= n; i++) {//increment "i" to 100(n)
            int  loopSum = sum + i; // had a println where return is and used "loopSum" to print out the value  but i cant return it now what am i doing wrong?
     
     
     
            return loopSum(n);
            }
    }
    	public static int formulaSum(int n) {
    		int formulaSum = n * (n+1) / 2;
            return formulaSum;
    }
    }
    I cant seem to figure out how to return the loop sum value to just do a simple print out of the value it should be 5050.
    Last edited by copeg; October 3rd, 2010 at 03:09 PM.

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Return from incrementing for loop?

    Yes thank you i know i need to change the method to int so i can return an Integer. i reposted the whole code in the "whats wrong with my code" forum the value of sum from what i linked when i use it in a println it is 5050. My teacher is trying to prove that a formula can be used instead of loops in certain instances to reduce the redundancy.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Return from incrementing for loop?

    Please do not repetitively ask the same question in multiple threads, and please use the code tags to make your code more readable. I have locked your other identical post, and merged the third with this.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Return from incrementing for loop?

    What does the program print out when you execute it?

  7. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Return from incrementing for loop?

    this is what i get for errors
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    This method must return a result of type int

    at IntSumReturnValues.loopSum(IntSumReturnValues.java :14)
    at IntSumReturnValues.main(IntSumReturnValues.java:7)
    also have a little notation for the "i++" saying it is a dead code....
    is the return taking it out of the incrementing process?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Return from incrementing for loop?

    What don't you understand about the error message?

    Don't know what your IDE does. Does the notation make sense?

  9. #9
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Return from incrementing for loop?

    I'm very new to programming and my teacher is very vague he just makes slide shows and they are all very unorganized this has been a problem with his classes for years from what i hear. So honestly i have not idea what it means I dont understand that when i put in the return it makes the i++ become dead unless the return is infact stopping the incrementing. and if that is the case i have no idea how to get the outcome to be returned.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Return from incrementing for loop?

    Play computer with your code. Do each statement one at a time using a piece of paper to keep track of the values of variables.
    See if the i++ part of the for loop control is executed.

    Why is the return inside of the loop?

  11. #11
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Return from incrementing for loop?

    I'm not really sure he hasn't gone over returns yet... he just assigned a homework assignment with it in it. I read a little on returns but still couldn't get it. It worked in the second method "formulaSum" so i thought it might have worked in the first one but I'm stuck because of the i++ and it saying that int loopSum isn't and integer?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Return from incrementing for loop?

    I have no more to say than past what I said back in post#10.


    it saying that int loopSum isn't an integer
    Please copy full text of error message and paste it here.

  13. #13
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Return from incrementing for loop?

    Okay, well thank you Norm for your help. I've changed the variable for "= sum+ i " to sumLoop and changed the return as well still having the same issue.

    I'm assuming because after it goes through the loop once it is trying to return the value. So i tried taking the return out of the for loop but leaving it in the method and it cant identify the variable i chose to use cause it is out of the loop. I really have no clue where to go from here?
    public static int loopSum(int n){
    		int sum = 0;
    		for (int i = 1; i <= n; i++) {//increment "i" to 100(n)
            int  sumLoop = sum + i; // had a println where return is and used "loopSum" to print out the value  but i cant return it now what am i doing wrong?
     
     
     
     
            }return sumLoop;
    }
    Last edited by helloworld922; October 3rd, 2010 at 07:48 PM.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Return from incrementing for loop?

    Why do you have the variable: sum and the variable sumLoop?
    You need to move the definition of sumLoop out of the {} the same as the definition for sum if you want to be able to access its value outside of the loop.

    What will the value of sumLoop be when the loop is done executing?

  15. The Following User Says Thank You to Norm For This Useful Post:

    mindlessn00b (October 3rd, 2010)

  16. #15
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Return from incrementing for loop?

    oh wow it was that simple!!!!! Thanks a ton Norm!!!!
    public class IntSumReturnValues {
     
    	public static void main(String[] args) {
            int n = 100;
           // int sumLoop= 
    		System.out.println("The sum of the numbers from 1 to "
    				+ n + " is " + loopSum(n) + ". Calculated with a loop"); // print out the "n" value and the loopSum value of 5050
     
    		System.out.println("The sum of the numbers from 1 to "
    				+ n + " is " + formulaSum(n) + ".  Calculated with the formula."); // print out the "n" value and the formulaSum value of 5050
     
     
        }
    	public static int loopSum(int n){
    		int sum = 0;
    		for (int i = 1; i <= n; i++) {//increment "i" to 100(n)
              sum = sum + i; // had a println where return is and used "loopSum" to print out the value  but i cant return it now what am i doing wrong?
     
     
     
     
            }return sum;
    }
    	public static int formulaSum(int n) {
    		int formulaSum = n * (n+1) / 2;
            return formulaSum;
    }
    }
    it was that simple haha it works now again thank you!
    Last edited by helloworld922; October 3rd, 2010 at 07:48 PM.

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Return from incrementing for loop?

    Glad you got it working.
    Good luck on the next project.

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Return from incrementing for loop?

    @daviddwilson
    Where are the rest of your posts? This post looks like it has lost its way and has ended up in the wrong thread.

    I think I recommended that you play computer and step through your code statement by statement to see if the i++ COULD EVER be executed.
    Did you do that? Do you think the i++ can be executed, ever. That is what your IDE is telling you: That code CAN NEVER be executed. Hence is dead/useless/misleading etc.

Similar Threads

  1. While (return value will terminate an iteration or loop?)
    By chronoz13 in forum Loops & Control Statements
    Replies: 3
    Last Post: April 27th, 2010, 09:05 AM
  2. ArrayList Unexpected Return
    By Cammack in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2010, 09:23 PM
  3. Incrementing every letter in a string that occupies an odd position.
    By haktheplanet in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2010, 04:45 AM
  4. Help - Return to menu screen
    By throzen in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 29th, 2009, 01:44 PM
  5. Prime number generator program missing return statement
    By 03EVOAWD in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 10th, 2009, 09:17 AM