Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8947435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:45:37+00:00 2026-06-15T12:45:37+00:00

I am trying to find the index of a string in a string array

  • 0

I am trying to find the index of a string in a string array using the Arrays.binarySearch() method however the method seems to be return the postion integer “-5” when looking for the string “Free”. Any idea why this would be?

String[] names = {"Arken","Ben","Darklark", "Free","group"};

void changeFriends(String uname, boolean b)
        {   // change a friend's "online" status

    Arrays.sort(names);
    int index = Arrays.binarySearch(names, uname);

    System.out.println("NAME OF ONLINE USER IS AT INDEX:" + index + "Name:" + uname);
        if(index > -1)
        {

             if(b == true)
            {
                loggedOn[index] = true;
            }
            else
            {
                loggedOn[index] = false;
            }
        }
        // call method to update buttons
        changeNameButtons();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:45:39+00:00Added an answer on June 15, 2026 at 12:45 pm

    Hmm, i just ran your code and i got the index 3.

    Arrays.sort(names);
    int index = Arrays.binarySearch(names, "Free");
    System.out.println(index);
    

    you probably are searching free or Free(with a trailing white space) instead of Free, in which case it returns -5.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find a clean way of referencing an array's index using
I am trying to find what array index position contains part of a string.
I am trying to find the index of a string in an array of
I'm using Lucene, and I'm trying to find a way to index and retrieve
Trying to find a clean way to find the index of an array item
I'm trying to figure out how to create a method to find a string
I'm trying to find out the index number of the last list item but
searching around trying to find a way to add index on suffix of a
I'm trying to find out a way to know the index of a anchor
Trying to find a good name for a method swapping each coordinate X and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.