• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Binary Search

 
Ranch Hand
Posts: 42
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having trouble with the Binary search for a string array. When my code calls binarySearch (line #43), I get an error:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
at SearchSort.binarySearch(SearchSort.java:75)
at SearchSort.main(SearchSort.java:50)
Java Result: 1

Which I believe means that I need a return statement at the end of my while loop. But in the examples in my text book, and all the examples of binary search I've found online, there is not a return statement at the end of the while loop.

My Binary Search Code:


My Entire Code:


Thanks for your time.
 
Ranch Hand
Posts: 87
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Which I believe means that I need a return statement at the end of my while loop. But in the examples in my text book, and all the examples of binary search I've found online, there is not a return statement at the end of the while loop.



It has less to do with the end of your while loop and more to do with the fact that the compiler wants an int return because your method calls for a return of int. What if, for some reason, the logic in the while loop is false? None of the if else logic would ever be checked and that leaves a method with no return value. You have to have a return value outside of the while loop and MORE importantly within the direct reach of the binarySearch() method.

Edit:
To make it really simple, the reason your code doesn't work is the same reason this wouldn't...

It doesn't matter if the while loop was true or false, the compiler would have an error. Can you blame it?



Just looking at your code, I wanted to share this video that shed some light to me. I am a new programmer, but I care very much about code readability ability to extend and I spend a great deal of time making this right. I believe it will pay me more in the future, by a lot!
https://www.youtube.com/watch?v=4F72VULWFvc
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scott M Summers wrote:I am having trouble with the Binary search for a string array. When my code calls binarySearch (line #43), I get an error:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
at SearchSort.binarySearch(SearchSort.java:75)
at SearchSort.main(SearchSort.java:50)
Java Result: 1



Please read this ... https://coderanch.com/how-to/java/FixAllCompilerErrorsBeforeRunningTheApplication.

It is not a good idea to run code when there are compiler errors. And the reasons are mentioned on that FAQ.

Henry
 
Michael Bruce Allen
Ranch Hand
Posts: 87
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My example above is better like this. Otherwise while(false) makes an unreachable statement even if you make a return value outside of the while. But either way it it needs a return value and if if logic is used then it needs a return for either way the logic goes. But again, like I said, I am kinda new. I was just trying to make a point.
 
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic