r/javahelp • • Apr 30 '26

Homework Help w/ Binary Search String Array

So, I've tried a couple of methods to figure this out- the original program was doing a binary search for an integer, and the assignment was to change it to a search for a string. I think it's pretty clear what the result is supposed to be (user inputs a string, program outputs what position in the array the string is in), but I can't wrap my head around how to get the string's placement in the array? Help would be greatly appreciated.

https://gist.github.com/lillyannx2/0e05f5f0c72af2b4ed52493b3cb83aac

1 Upvotes

7 comments sorted by

•

u/AutoModerator Apr 30 '26

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/rupertavery64 Apr 30 '26

For binary search to work, the list must be sorted.

Do you understand the binarysearch algorithm itself?

1

u/enderwoah Apr 30 '26

Not really, to be honest. Does it need to be sorted in alphabetical order or is this a utility I'm meant to use?

→ More replies (1)

2

u/LetUsSpeakFreely Apr 30 '26

In order for a binary search to work there data needs to be sorted first. The simple way to do that is calls Arrays.sort(). However, that will only get you the position in the sorted array, not the source array. You would need to store the original array prior to sorting it. For that, you'll have to load the array into a map with the string as the key and index as the value, and you can use Streams. Then you just gotta put it all together.

  1. build the map
  2. sort the array
  3. search the array
  4. lookup the value in the map
  5. output the value from the map.

1

u/jlanawalt May 01 '26

Where is BinarySearch(Integer)? (BinarySearch(<T>))

That would make it more clear how BinarySearch(String) should act (and hopefully that gives a hint at how one can turn into the other if well designed.