Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Binary Search  RSS feed

 
Scott M Summers
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Michael Bruce Allen
Ranch Hand
Posts: 87
2
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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 ... http://www.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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!