File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Compiler: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compiler: "This method must return a result of  type int" What to do?" Watch "Compiler: "This method must return a result of  type int" What to do?" New topic
Author

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

Alex Bruhart
Greenhorn

Joined: Apr 20, 2008
Posts: 8
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Alex Bruhart
Greenhorn

Joined: Apr 20, 2008
Posts: 8
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

Joined: Jan 17, 2006
Posts: 1296
Don't use == to compare Strings, use the equals() method.


[ April 20, 2008: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Garrett Rowe:
Don't use == to compare Strings, use the equals() method.


Thanks, Garrett, I missed that!
Alex Bruhart
Greenhorn

Joined: Apr 20, 2008
Posts: 8
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

Joined: Oct 27, 2005
Posts: 19794
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compiler: "This method must return a result of type int" What to do?