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 4007942
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:41:35+00:00 2026-05-20T08:41:35+00:00

Sorry if this is a repeat, but I’ve been looking for an answer to

  • 0

Sorry if this is a repeat, but I’ve been looking for an answer to this question, but I can’t find one anywhere and my last question was for the same code but with a different problem.

I need to find a string inside an array, but all the Strings in the array are 16 characters long and the search key will not be 16 characters when entered by the user. Every attempt I’ve made at this thus far has ended with the String not being found, though I know it is in the array. I think it has something to do with the trailing spaces after the actual string text in the array, but I’m not sure how to handle it.

This is my search statement so far. I’ll note that my compareTo() statement does compare this.name to other.name, so I’m quite confused.:

case 'b':
      System.out.println();
      System.out.println("Please enter a customer name:");

      String search = kb.nextLine(); //read the user's search
      int place; //location of result

      Arrays.sort(A);

      Customer searchCust = new Customer(search);

      place = Arrays.binarySearch(A,searchCust);

      if (place <= 0)
        System.out.println("Cannot find customer named " + search);
     else
     {
       System.out.println("Customer found:");  
       System.out.println(A[place-1]);
        break;


 public int compareTo(Customer a)
 {
  return this.name.compareTo(a.name);
 }   //end compareTo
  • 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-05-20T08:41:36+00:00Added an answer on May 20, 2026 at 8:41 am

    Edit: after clarification of the question.

    So, the problem is that your users are entering something like

    "Smith"

    but your array elements are always padded to 16-characters long and might have the search term somewhere in the middle, like

    "John Smith      "

    then I would recommend just looping through the array and doing a .contains() on each element.

    Binary search is an optimization which I would only consider if performance shows itself to be a problem, because it introduces quite a lot of complication.

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

Sidebar

Related Questions

sorry this is such a simple question but I can't figure it out. How
Sorry I can't put this code live anywhere at the moment but hopefully the
Sorry if this is a repeat question. I've looked at other threads, but while
Sorry this is a basic question, but all my research just barely missed answering
Ok sorry this might seem like a dumb question but I cannot figure this
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry if this sounds like a really stupid question, but I need to make
Sorry if I repeat my question but I have still had no clues of
I've got a simple one but haven't been able to find an example of
I am sorry if this is a repeat question. How do I write a

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.