William Momentai Yang
William Momentai Yang

Reputation: 11

Missing Return Statement - int[]

I'm programming a method in Java (first time doing so) and I came across a little error when I compiled and I still don't understand where I'm going wrong, despite debugging hundreds of times. Finally I"m turning to the Internet for help.

Here's the section of my code with problems:

    public int[] sortArray(String [] args) {
        String temp = "";
        for(int x = 0; x < args.length; x++){
            for(int y = 1; y < args.length - 1; y++){
            if(Integer.parseInt(args[y-1])>Integer.parseInt(args[y])){
                temp = args[y-1];
                args[y-1] = args[y];
                args[y] = temp;
            }
        }
    }
}

Every time I compile it provides the error: Missing return statement When I put a "return 0;" into the code before the last '}', it gives an error of the wrong type of return, i.e. 0 is an int and I need an int[] return.

Upvotes: 0

Views: 1494

Answers (8)

phalgun kumar dandu
phalgun kumar dandu

Reputation: 29

If you dont want to return any value, then change the return type as void. If you want to return any thing, then use appropriate datatype, in your case you should return int array variable.

Upvotes: 0

Zhivko Draganov
Zhivko Draganov

Reputation: 1253

In java parameters comes with reference like (char* args) in C/C++. So you don't need to return anything in this section just use the args array in caller and it will be fine :)

Upvotes: 0

Abhishek Choudhary
Abhishek Choudhary

Reputation: 8395

  public String[] sortArray(String [] args) {
            String temp = "";
            for(int x = 0; x < args.length; x++){
                for(int y = 1; y < args.length - 1; y++){
                if(Integer.parseInt(args[y-1])>Integer.parseInt(args[y])){
                    temp = args[y-1];
                    args[y-1] = args[y];
                    args[y] = temp;
                }
            }
        }
            return args;
    }

or use void type

Upvotes: 1

barsju
barsju

Reputation: 4446

You probably just want to return args or change your method to void

Upvotes: 0

UmNyobe
UmNyobe

Reputation: 22910

There is no return statement in your function! Thus the compilation error... It seems you are directly working on args[], so don't return anything (void function)

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502406

What is the method meant to return? Your method sorts in place - why does it need to return anything at all? You can just make it a void method:

public void sortArray(String[] args) {
    for (int x = 0; x < args.length; x++) {
        for (int y = 1; y < args.length - 1; y++) {
        if (Integer.parseInt(args[y-1]) > Integer.parseInt(args[y])) {
            // Note change of declaration location: in general, declare
            // variables with minimal scope
            String temp = args[y - 1];
            args[y - 1] = args[y];
            args[y] = temp;
        }
    }
}

(Note that you're doing a lot of unnecessary parsing here, and also you never use the value of x in your inner loop... is that deliberate?)

Upvotes: 2

Korhan Ozturk
Korhan Ozturk

Reputation: 11318

Change the signature of your sort method to void. Otherwise it expects you yo return your sorted int array at the end of your method.

If you want to return your sorted String array, change your method as the following:

public String[] sortArray(String [] args) {
    String temp = "";
    for(int x = 0; x < args.length; x++){
        for(int y = 1; y < args.length - 1; y++){
        if(Integer.parseInt(args[y-1])>Integer.parseInt(args[y])){
            temp = args[y-1];
            args[y-1] = args[y];
            args[y] = temp;
        }
     }
  }
  return args;
}

Upvotes: 0

KARASZI Istv&#225;n
KARASZI Istv&#225;n

Reputation: 31467

You should return with an integer array not an integer or change return type of the method signature to void.

public void sortArray(String [] args) {
  // ...
}

Upvotes: 4

Related Questions