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];
}
}
}
}
}

Recommended Answers

All 12 Replies

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

Yours Sincerely

Richard West

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

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.

Thank you very much for ur help all of u, it is working now!! Thanks!

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

}

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.

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);
       [COLOR="Red"]string mySum;
       //Copy total to mySum
       retun mySum;[/COLOR]
  }// HERE IT SAYS THAT I AM MISSING A RETURN STATEMENT

} 

end quote.

You need to make a string out of the integer total and return it

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();

      }
   }

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();

      }
   }

end quote.

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.

that's because your class is filled with errors. you are telling the compiler that you are returning an int, both in getOrder() and displayOrders(), but you have no return statement, nor do you have a variable in which you store the value in your main method, or a print in which the value is shown.

next to that: if you would've looked at the post you're answering to, you would've seen it's a post that dates back to 2004. There is no use what so ever to revive this thread. if you have a question, just create a new thread.

and since I've answered your question in my first paragraph, and give you enough info to correct your code, there is no need to create another thread with this question.

this is for a class and it is saying that I am missing a return statement. I am hoping that you will have some ideas. I have read the thread, but I am still confused on how to fix it

public class Garden
{
    // instance variables - replace the example below with your own
    double VP;
    double N10;
    double N6;
    double N4;
    /**
     * Constructor for objects of class Garden
     */
    public Garden(double VolumeToPack, double NumberOf10Bags, double NumberOf6Bags, double NumberOf4Bags)
    {
        VP=VolumeToPack;
        N10=NumberOf10Bags;

        N6=NumberOf6Bags;
        N4=NumberOf4Bags;
    }
    public double computeNumberOf10Bags()
    {
       return(VP/10.0);
    }
public double computeNumberOf6Bags()
    {
        return(VP%10.0)/6.0;
    }
public double computeNumberof4Bags()
    {
        return((VP%10.0)%6.0);
    }
public double computeDeliveryCost()
    {
        if (VP<=12)
        {
               return(5.00);
        }       
        else if (VP>12)
        {
            return(5.00+(VP-12)*0.25);
        }    
    }
}

I will be happy to explain the answer - your confusion here is actually reasonable - if you'll be kind enough to
a)start a new thread for this question, since we don't like new questions in old threads, it's contrary to the house style
and
b) put code tags around your code, so it comes out formatted legibly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.