Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: error: missing return statement

  1. #1
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Question error: missing return statement

    I am getting this error and I'm not quite sure why...

    java:69: error: missing return statement
    }
    ^

    That's the entire error, and here is the code:

    	public int get (String s)
    	{
    		final int notfound = -1;
     
    		if (head == null)
    		{
    			System.out.println (notfound);
    		}
    		else
    		{
    			ListNode current = head;
    			while (current.next != null)
    			{
    				if (current.thepair.getString() == s)
    				{
    					System.out.println(current.thepair.getInt());
    				}
    				current = current.next;
    			}
    		}
    		System.out.println (notfound);
    	}

    So basically, this is part of a linked list. Each node in the list stores a pair (an integer and a string).
    The get() method takes a string and traverses the list until it finds a pair which contains the same string.
    If the list is empty, or if the string isn't found, it returns -1. If it finds the string, however, it returns the integer that is paired with it.

    Any help would be much appreciated.
    thanks!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: error: missing return statement

    public int get (String s)
    The method definition REQUIRES that the method always return an int value when the method ends.
    Make sure that all exits from the method return an int value.

    If you don't want the method to return a value, change int to void in the method definition.
    However, a method named get() would be expected to return something.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    bassie (January 6th, 2013)

  4. #3
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: error: missing return statement

    Hello again Norm, and thanks for the response.

    I thought that might be the case, but as far as I can tell this method can return nothing but an int.

    if the list is empty (head == null), it returns notfound (final int -1).
    else, the list is traversed (while the next node !=null) if during this traversal a match is found, the method getInt() is called (returning an int).
    if the while loop is exited, notfound is returned, as this means that the end of the list has been reached without a match being found.

    I am so confused!

    --- Update ---

    I just edited the method slightly;

    	public int get (String s)
    	{
    		final int notfound = -1;
     
    		if (head == null)
    		{
    			return notfound;
    		}
    		else
    		{
    			ListNode current = head;
    			while (current.next != null)
    			{
    				if (current.thepair.getString() == s)
    				{
    					System.out.println(current.thepair.getInt());
    				}
    				current = current.next;
    			}
    			return notfound;
    		}
    	}

    So now the notfounds are returned rather than printed.

    The code compiles now, but when I test the method with the following statements:

    	{
    		PairList list = new PairList();
     
    		list.get ("name");
     
    		System.out.println("The List : ");
     
    		list.add (new Pair("name",5));
    		list.add (new Pair("jim",20));
    		list.add (new Pair("cat",8));
    		list.add (new Pair("word",4));
    		list.add (new Pair("string",6));
    		list.print();
     
    		list.get ("name");
    		list.get ("nameee");
    		list.get ("string");
    	}

    The output I get is:

    name5
    jim20
    cat8
    word4
    string6
    5

    But according to what the code should be doing, I am missing two -1s and a 6!!

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: error: missing return statement

                    list.get ("name");
    		list.get ("nameee");
    		list.get ("string");
    The code needs to do something with the int value returned by the get() method. The get() method is called three times and ignores what is returned all three times. You could assign what is returned to an int variable that could then be tested and used later in the program.

    The println statement should include an id String as well as the number that is being printed. Just a number doesn't say what the number is.
    System.out.println("thepair="+current.thepair.getInt());

    if (current.thepair.getString() == s)
    Use the equals() method when comparing Strings, not the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    bassie (January 6th, 2013)

  7. #5
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: error: missing return statement

    Quote Originally Posted by Norm View Post
    The code needs to do something with the int value returned by the get() method. The get() method is called three times and ignores what is returned all three times. You could assign what is returned to an int variable that could then be tested and used later in the program.
    But why would the code ignore 3 out of 4 of the get() tests? it returns the "name" integer when called the second time, but not the "string" integer or the two notfounds!

    also, if I swap the list.get("string"); statement with the second list.get("name") statement, nothing is returned, leading me to believe that my traversal system is somehow flawed...

    I'm going to try alter the get() method as I don't understand what is going on here. All I'm trying to do is test whether the list actually works, the output that I need printed out is simply so that I can test it!

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: error: missing return statement

    But why would the code ignore 3 out of 4 of the get() tests?
    Here's what the code does:
    The code calls the get() method
    The get() method does a search and returns a value;
    The returned value is ignored. The returned int needs to be assigned to a variable.

    nothing is returned
    How do you know anything is returned? As I said above, all the values returned by get() are ignored.

    the output that I need printed out is simply so that I can test it!
    I use println statements all the time for debugging. Their usage has nothing to do with what a method returns.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Missing return statement
    By mrroberts2u in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2011, 06:11 AM
  2. Missing return statement
    By Stn in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 1st, 2011, 08:03 PM
  3. Missing Return Statement in If-elseIf-else
    By chronoz13 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 12th, 2009, 07:01 AM
  4. Prime number generator program missing return statement
    By 03EVOAWD in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 10th, 2009, 09:17 AM
  5. Error of "Class has no return statement"
    By mdstrauss in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 2nd, 2009, 12:00 PM