• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic