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:
Code java:
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!
Re: error: missing return statement
Code :
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.
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;
Code java:
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:
Code java:
{
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!!
Re: error: missing return statement
Code :
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.
Code :
System.out.println("thepair="+current.thepair.getInt());
Code :
if (current.thepair.getString() == s)
Use the equals() method when comparing Strings, not the == operator.
Re: error: missing return statement
Quote:
Originally Posted by
Norm
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!
Re: error: missing return statement
Quote:
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.
Quote:
nothing is returned
How do you know anything is returned? As I said above, all the values returned by get() are ignored.
Quote:
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.