DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   missing return statement (http://www.daniweb.com/forums/thread9534.html)

i_me_roo Aug 18th, 2004 9:10 am
missing return statement
 
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];
}
}
}
}
}

freesoft_2000 Aug 18th, 2004 3:05 pm
Re: missing return statement
 
hello everyone,
Your class main is missing and you added an extra bracket at the end

Yours Sincerely

Richard West

cosi Aug 18th, 2004 9:18 pm
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

nanosani Aug 19th, 2004 4:16 am
Re: missing return statement
 
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.

i_me_roo Aug 22nd, 2004 1:24 pm
Re: missing return statement
 
Thank you very much for ur help all of u, it is working now!! Thanks!

JRabbit2307 Oct 8th, 2009 5:48 am
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

}

javaAddict Oct 8th, 2009 6:04 am
Quote:

Originally Posted by JRabbit2307 (Post 1008024)
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.

ithelp Oct 9th, 2009 6:21 am
Quote:

Originally Posted by JRabbit2307 (Post 1008024)
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


All times are GMT -4. The time now is 1:29 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC