943,617 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 51002
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 18th, 2004
0

missing return statement

Expand Post »
Hi guys,

Im trying to compile this class below, but its just not working, it keeps sating 'missing return statement' int the method declaration line. Can u see to check if there is anything missing in my code or if iv done something wrong??

Thanks!


public class swapping {

public int trade(int []a, int b){ //says theres a missing return statement
int temp=0;
for (int n = 1; n < b; n++) {
for (int m = 0; m < b - 1; m++) {
if (a[m] > a[m] + 1) {
temp = a[m];
a[m] = a[m+1];
a[m+1] = temp;
return a[m];
}
}
}
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
i_me_roo is offline Offline
11 posts
since Aug 2004
Aug 18th, 2004
0

Re: missing return statement

hello everyone,
Your class main is missing and you added an extra bracket at the end

Yours Sincerely

Richard West
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Aug 18th, 2004
0

Re: missing return statement

Well I'm sure that i_me_roo probably has a main and extra bracket at the end. The compiler complaint was about return statements.

i_me_roo if you look at the code, you'll see that it is possible to go straight through the function without hitting the return statement. This is what the compiler is complaining about. The function returns an int, so all paths of execution must have a return value;

My suggestion is to have a 'return ERROR', or 'return 0', or 'return -1' after the for loops if you never intend to reach that line of execution. This is the best advice I can think of since I have no idea about the context of the function you wrote.


Ed
Reputation Points: 17
Solved Threads: 1
Junior Poster
cosi is offline Offline
153 posts
since Aug 2004
Aug 19th, 2004
1

Re: missing return statement

Java Syntax (Toggle Plain Text)
  1. public class swapping {
  2.  
  3. public int trade(int []a, int b){ //says theres a missing return statement
  4. int temp=0;
  5. for (int n = 1; n < b; n++) {
  6. for (int m = 0; m < b - 1; m++) {
  7. if (a[m] > a[m] + 1) {
  8. temp = a[m];
  9. a[m] = a[m+1];
  10. a[m+1] = temp;
  11. return a[m];
  12. }
  13. }
  14. }
  15. return 0;
  16. }
  17. }

Now it'll run well.
Team Colleague
Reputation Points: 45
Solved Threads: 56
Unauthenticated Liar
nanosani is offline Offline
1,767 posts
since Jul 2004
Aug 22nd, 2004
0

Re: missing return statement

Thank you very much for ur help all of u, it is working now!! Thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
i_me_roo is offline Offline
11 posts
since Aug 2004
Oct 8th, 2009
-3

missing return statement

hi everyone i'm new to programming and i have to say its fun and frustrating at the same time could anyone help me fix this??



// Exercise5_2.java: Create a method for summarizing digits in an int

public class Exercise5_2 {
public static void main(String[] args) {
// Create a Scanner
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter a number: ");
long value = input.nextInt();
String t = getSum(value);
System.out.print(t);

}

public static String getSum(long value) {

//FILL IN THE METHOD
long remainingAmount = value;

long n1 = remainingAmount / value;
remainingAmount = remainingAmount % value;
long n2 = remainingAmount / value;
remainingAmount = remainingAmount % value;
long n3= remainingAmount / value;
remainingAmount = remainingAmount % value;
long n4 = remainingAmount / value;
remainingAmount = remainingAmount % value;
long n5 = remainingAmount / value;
remainingAmount = remainingAmount % value;
long n6 = remainingAmount / value;
remainingAmount = remainingAmount % value;
long n7= remainingAmount / value;
remainingAmount = remainingAmount % value;
long n8 = remainingAmount / value;
remainingAmount = remainingAmount % value;
long n9 = remainingAmount;

// Do the math to add digits together

long total = (n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9);
System.out.println("The sum of digits for " + value + " is " + total);

}// HERE IT SAYS THAT I AM MISSING A RETURN STATEMENT

}
Reputation Points: 10
Solved Threads: 0
Light Poster
JRabbit2307 is offline Offline
32 posts
since Oct 2009
Oct 8th, 2009
0
Re: missing return statement
hi everyone i'm new to programming and i have to say its fun and frustrating at the same time could anyone help me fix this??
This is a 5 year old thread. That were you thinking posting here? Do you random peek threads and start writing irrelevant things in them?

If it says it is missing a return statement, then add one since your method is declared to return a String.
If you have any more questions start a new thread.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Oct 9th, 2009
0
Re: missing return statement
hi everyone i'm new to programming and i have to say its fun and frustrating at the same time could anyone help me fix this??



// Exercise5_2.java: Create a method for summarizing digits in an int

public class Exercise5_2 {
public static void main(String[] args) {
// Create a Scanner
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter a number: ");
long value = input.nextInt();
String t = getSum(value);
System.out.print(t);

}

public static String getSum(long value) {

//FILL IN THE METHOD
long remainingAmount = value;

long n1 = remainingAmount / value;
remainingAmount = remainingAmount % value;
long n2 = remainingAmount / value;
remainingAmount = remainingAmount % value;
long n3= remainingAmount / value;
remainingAmount = remainingAmount % value;
long n4 = remainingAmount / value;
remainingAmount = remainingAmount % value;
long n5 = remainingAmount / value;
remainingAmount = remainingAmount % value;
long n6 = remainingAmount / value;
remainingAmount = remainingAmount % value;
long n7= remainingAmount / value;
remainingAmount = remainingAmount % value;
long n8 = remainingAmount / value;
remainingAmount = remainingAmount % value;
long n9 = remainingAmount;

// Do the math to add digits together

long total = (n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8 + n9);
System.out.println("The sum of digits for " + value + " is " + total);
string mySum;
//Copy total to mySum
retun mySum;

}// HERE IT SAYS THAT I AM MISSING A RETURN STATEMENT

}
You need to make a string out of the integer total and return it
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Feb 2nd, 2011
0
Re: missing return statement
i have the same problem! missing return statement
class MOrders
{
int nooforders;// Data members of the class
public int getorder()
{
nooforders = 500;
}

public int displayorders()
{
System.out.println("The number of orders to be delivered:"+ nooforders);
}
public static void main(String args[])
{
MOrders obj = new MOrders ();
obj.getorder();
obj.displayorders();

}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ramhota is offline Offline
1 posts
since Feb 2011
Feb 2nd, 2011
0
Re: missing return statement
Click to Expand / Collapse  Quote originally posted by ramhota ...
i have the same problem! missing return statement
class MOrders
{
int nooforders;// Data members of the class
public int getorder()
{
nooforders = 500;
}

public int displayorders()
{
System.out.println("The number of orders to be delivered:"+ nooforders);
}
public static void main(String args[])
{
MOrders obj = new MOrders ();
obj.getorder();
obj.displayorders();

}
}

Did you bother to read the rest of the thread with the answers. You resurrected an old thread just to post a question that has already been answered? Are you too lazy to read the suggestions and advices given and you want some one just to tell you again the answer, instead of you spending 5 minutes to read the rest of the thread?

If it says "missing return statement" then you haven't put a "return" statement. Why you should put one? Read the rest of the thread.
Also, the errors that you get tell you exactly where the error is at your code. Read those errors.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: doubt array
Next Thread in Java Forum Timeline: Tic Tac Toe Code Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC