| | |
missing return statement
![]() |
•
•
Join Date: Aug 2004
Posts: 11
Reputation:
Solved Threads: 0
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];
}
}
}
}
}
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];
}
}
}
}
}
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
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
Java Syntax (Toggle Plain Text)
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]; } } } return 0; } }
Now it'll run well.
•
•
Join Date: Oct 2009
Posts: 32
Reputation:
Solved Threads: 0
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
}
// 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
}
0
#7 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??
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
0
#8 Oct 9th, 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);
string mySum;
//Copy total to mySum
retun mySum;
}// HERE IT SAYS THAT I AM MISSING A RETURN STATEMENT
}
![]() |
Similar Threads
- Missing } return statement (Java)
- Compiler says: "missing return statement"...even though I have a return statement! (Java)
- Missing return Statement (Java)
- missing return statement (Java)
- missing return statement (Java)
- missing return statement 106 and 123, any ideas?? (Java)
Other Threads in the Java Forum
- Previous Thread: i need help creating a simple game.
- Next Thread: Multiple HTTPSessions in Struts
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp functiontesting game gameprogramming givemetehcodez graphics gui health html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list mac main map method methods mobile myregfun netbeans notdisplaying number online printf problem program project qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor






