Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compiler: "This method must return a result of type int" What to do?

 
Alex Bruhart
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, this is puzzling me. I will post the whole program and will point out the problem spot.


Now, the problem comes from the nameSearch method. It keeps saying "This method must return a result of type int," but I have two returns there.

What is going on? Thanks for the help.
[ April 20, 2008: Message edited by: Alex Bruhart ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Although you might know better, as far as the compiler can tell, it's possible for control to read the end of the method without ever seeing a return statement. For example, trivially, what if array inOrder has length 0? Then the for loop's body is never executed at all, and the method immediately tries to return -- but can't, as there's no "return" statement.

So if you added a "return -1" at the very end, you'd be good. You could then replace the "return -1" inside the loop with "break", so it'd just jump to the outside return.
 
Alex Bruhart
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, thanks for your quick and kind response.

Do you mean doing something like this?



If so, now when I tried to run the program, even if it is supposed to return i, it returns -1.

Thanks again.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use == to compare Strings, use the equals() method.


[ April 20, 2008: Message edited by: Garrett Rowe ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Garrett Rowe:
Don't use == to compare Strings, use the equals() method.


Thanks, Garrett, I missed that!
 
Alex Bruhart
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again, I really appreciate it.

I love learning new languages, but there is so much to remember! You should have seen me earlier trying to solve this problem. Before I remembered the string comparisons way of comparing strings, I had this big complicated nested nested for loop with ifs and elses comparing each letter of each word, haha.

Anyway, thanks again, I really appreciate it.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can remove the else part, because the for-loop guard ensures that i will never be larger than inOrder.length; as soon as it is equal the loop stops.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic