•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,592 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,529 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 18334 | Replies: 4
![]() |
•
•
Join Date: Aug 2004
Posts: 11
Reputation:
Rep Power: 5
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
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- missing return statement (Java)
- missing return statement x( (Java)
- missing return statement (Java)
- missing return statement 106 and 123, any ideas?? (Java)
Other Threads in the Java Forum
- Previous Thread: Java Manifest File
- Next Thread: Interfacing



Linear Mode