1.11M Members

Missing Return Statement Help


In the last line of this method I am getting a missing return statement error, and I cannot figure out why. Any ideas?

public static int parameter(int g){
        Scanner console=new Scanner(System.in);
        String hint=console.next();

            return g;

        }else if(hint.equals("high")){
            return g;

        }else if(hint.equals("correct")){
            return g;

The method is defined to return an int value. The compiler sees that there could be a possibility of the code getting to the end of the if/else if statements and exiting the method without returning an int value. What if hint = "XXXX"?
Add a return with an int value at the end of the method.


What happends if hint is none of low/high/correct? It executes none of your return statements and drops down to line 17 without returning an int.


After doing that, no matter what I enter(low, high, correct), it returns g with no calculation. Is the format I've used the correct format to match with user input?


To see what the computer sees print out the value of hint with a println after it is read.
Be sure to add String delimiters before and after the variable so you can see all its characters:

The equals() method is used to compare String values.


equals() method compares two Strings.
If you have given input High / Low / correct, it only matches with hint variable.
What you have stored in hint variable String ot integer?
If you give any input instead of these 3 like bbb or aaa, it will not execute any of these case,and also return nothing.

so find what you are storing in hint variable.


To solve this problem, simply set a variable equal to all of the return values in each of the if/else-if statements and then return that variable outside of the if/else-if blocks.


Zach&Kody: re-read your post, and you'll see why that's impossible. (and certainly not what is wanted).
either you return a default value if none of the cases are true, or throw an Exception.

This article has been dead for over six months: Start a new discussion instead
Start New Discussion
Tags Related to this Article