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
Adeclaration.search = search.replaceAll("(\\r|\\n)", "");and it didn't change my results any. I feel like I'm still missing something pretty huge here. :/Customerclass. ParticularlycompareToandequals.