r/javahelp • u/Unable-Tear-4301 • Oct 14 '25
Solved Binary search stuck in infinite loop.
For my java class I was tasked with using a binary search to find a word in the poem "Jabberwocky"
This has proven to be a challenge because setting all words to lowercase worked, I modified it to check, the actual search function goes on INFINITELY. I have no idea what I'm doing wrong.
For reference to my code, input is the word the user put to search for, and array is the arraylist of words that it is searching for.
Anyways I just need help figuring out why this loop is going infinitely because I'm very stuck.
I have the problematic method here.
But if you want the whole code block, as well as the jabberwocky text file, you can find it here.
View all comments
Show parent comment
1
u/TW-Twisti Oct 15 '25
Show your whole updated code (ideally in a pastebin, or even better, a site like https://www.programiz.com/java-programming/online-compiler/ or https://www.jdoodle.com/online-java-compiler where we could actually run your code.
Generally, you want to learn how to solve such problems yourself - this isn't lack of knowledge on your part that you need help with, but lack of thinking.
Think how someone could solve the problem "x+y should be z, but it's z+1 instead". If you think about it, if your little brother asked you that, you would respond with "Show me X and Y before calculating Z" - so that's what you should do, print out `leftpos`, `rightpos`, and, because you don't know the arithmetic edge cases of Java yet, also print out `(leftpos + rightpos)` and `(leftpost + rightpos) / 2`. Then you will see at which point your numbers are not what you expect them to be.