We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,304 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

        if(hint.equals("low")){
            g=(g/2)+g;
            return g;

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

        }else if(hint.equals("correct")){
            g=g;
            return g;
        }
}
6
Contributors
7
Replies
1 Week
Discussion Span
5 Months Ago
Last Updated
8
Views
joe.crook.9
Newbie Poster
3 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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.

JamesCherrill
... trying to help
Moderator
8,525 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

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?

joe.crook.9
Newbie Poster
3 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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:
System.out.println("hint="+hint+"<");

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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.

jalpesh_007
Posting Whiz
336 posts since Sep 2010
Reputation Points: 4
Solved Threads: 36
Skill Endorsements: 4

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
Newbie Poster
17 posts since Feb 2011
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 0

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.

stultuske
Industrious Poster
4,379 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0774 seconds using 2.67MB