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

  • Home
  • SEARCH
  • 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 7902711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:34:25+00:00 2026-06-03T09:34:25+00:00

I am a beginner in Java and is learning to use array. I understand

  • 0

I am a beginner in Java and is learning to use array. I understand that when using the binary search method of Array, it will return a negative number if the entry is not found. However, in the following code, I am getting a negative number returned for 9, 10, and 11.

I am wondering if anyone could help point out what I am doing wrong? thanks!

   String [] oneToSixteen = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16"};

   System.out.println("Searching for 7: "+ Arrays.binarySearch(oneToSixteen, "7"));
   System.out.println("Searching for 8: "+ Arrays.binarySearch(oneToSixteen, "8"));
   System.out.println("Searching for 9: "+ Arrays.binarySearch(oneToSixteen, "9"));
   System.out.println("Searching for 10: "+ Arrays.binarySearch(oneToSixteen, "10"));
   System.out.println("Searching for 11: "+ Arrays.binarySearch(oneToSixteen, "11"));

The output i get are:

Searching for 7: 6
Searching for 8: 7
Searching for 9: -17
Searching for 10: -2
Searching for 11: -2

Any help would be much appreciated.

  • 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-03T09:34:26+00:00Added an answer on June 3, 2026 at 9:34 am

    This is because your array is an an array of String and not int and it is not sorted.

    The documentation clearly states that the array being searched must be sorted and if it isn’t the results are undefined.

    To sort the array you can use the sort method of the Arrays class.

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

Sidebar

Related Questions

I'm Java beginner, but I thought that when using try-catch-finally I don't have to
I'm a Java beginner. I already created a simple GUI application that display will
This is a real beginner question (I'm still learning the Java basics). I can
I am beginner in Cryptography concept. I want connect to a server that use
I am a beginner in Java and do not understand what if(!s.add(a)) means in
I am a beginner learning Java. I have two classes, public class myClassA {
I'm a beginner java programmer. I'm working on an application that decrypts some data.
I'm beginner in Java, so, i'm sorry if the question will be too simple
I am a very beginner to Java, recently trying to use some existing libraries
Possible Duplicate: servlet vs filter I am java beginner. Now I am learning about

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.