missing return statement

Reply

Join Date: Aug 2004
Posts: 11
Reputation: i_me_roo is an unknown quantity at this point 
Solved Threads: 0
i_me_roo i_me_roo is offline Offline
Newbie Poster

missing return statement

 
0
  #1
Aug 18th, 2004
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];
}
}
}
}
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 7
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: missing return statement

 
0
  #2
Aug 18th, 2004
hello everyone,
Your class main is missing and you added an extra bracket at the end

Yours Sincerely

Richard West
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 153
Reputation: cosi is an unknown quantity at this point 
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: missing return statement

 
0
  #3
Aug 18th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 1,749
Reputation: nanosani is an unknown quantity at this point 
Solved Threads: 55
Team Colleague
nanosani's Avatar
nanosani nanosani is offline Offline
Unauthenticated Liar

Re: missing return statement

 
0
  #4
Aug 19th, 2004
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 11
Reputation: i_me_roo is an unknown quantity at this point 
Solved Threads: 0
i_me_roo i_me_roo is offline Offline
Newbie Poster

Re: missing return statement

 
0
  #5
Aug 22nd, 2004
Thank you very much for ur help all of u, it is working now!! Thanks!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 32
Reputation: JRabbit2307 is an unknown quantity at this point 
Solved Threads: 0
JRabbit2307 JRabbit2307 is offline Offline
Light Poster

missing return statement

 
-1
  #6
Oct 8th, 2009
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

}
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,619
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 218
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
0
  #7
Oct 8th, 2009
Originally Posted by JRabbit2307 View Post
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.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,810
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 117
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso
 
0
  #8
Oct 9th, 2009
Originally Posted by JRabbit2307 View Post
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
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC