1,105,402 Community Members

For Each Loop

Member Avatar
KRUX17
Junior Poster in Training
50 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

Why does this piece of code throw me an error saying: cannot return a value from method whose result type is void?

public void orderTotal()
       {
       int total;

       for (Order filename: Orders) {

         total = filename.getQuantity(); 


        }

        return total;
   }
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

Please post the full text of the error message that shows where the error occurs.
What statement causes the exception?

method whose result type is void?

The posted method is defined with return type of void on line 1. It can not return a value on line 12.
Change its return type to be the same type as total if you want to return a value.

BTW total won't be an accumulation of values, it will hold the last value received in line 7

Member Avatar
KRUX17
Junior Poster in Training
50 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

The error occurs on the return total; The getQuantity() method is being called from another class.

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

We cross posted. See my last post.

Member Avatar
KRUX17
Junior Poster in Training
50 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you, I can't believe I didn't see that.

Question Answered as of 1 Year Ago by NormR1
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article