473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Binary search, for strings in an array

praclarush
Hey everyone, I’m new at Java and I’m having a bit of trouble. I have an assignment in which I have an Array of objects. The user is to input a name, which then needs to be found in the Array so that the information for that name is displayed. My problem is that i was told to use a binary search and I'm not supposed to use the Arrays class. I know how to do a binary search when the array contains numbers. But I am unsure of how to do it when it contains strings. Can any one give me an example of how I can do this or is it impossible to do with out the Arrays class?

Thanks,
Praclarush
Oct 26 '07 #1
4 3149
JosAH
11,448 Expert 8TB
You should have asked your question in the Java Forum section, not in the Java
Articles section. I'll move your question for you.

kind regards,

Jos
Oct 26 '07 #2
opps sorry i thought it was in the forums area my bad

thanks,
Praclarush
Oct 26 '07 #3
Ganon11
3,652 Expert 2GB
Well, instead of testing for equality with '==', use the .equalsIgnoreCase() method of the String class. Other than that, it works exactly the same.

In fact, you can create a generalized binarySearch method that accepts a Comparable object (Comparable is an interface which many classes support). You can't use .equalsIgnoreCase in this case, but you can still use .equals() (which is required by the Comparable interface).

You will also need to use the .compareTo method to see if the current string is less than or greater than the search String. (In fact, it might be easier just to call .compareTo once and use that value for your tests).

You can check out all of these methods at the Java API here.

EDIT: Whoops, if you use a Comparable object, you can't use .equals(). It's not supported. But .compareTo() does all you need, and this can still work.
Oct 26 '07 #4
Ok, I managed to get it working, thanks a lot for the help Ganon11

Thanks again,
praclarush
Oct 26 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
3
by: Jeffrey D. Gordon | last post by:
I'm wanting to replace Field Values in an existing PDF, I've done this with PHP by doing a replace in the file. I've been able to read the file in a byte array in c# but all my attempts to...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
10
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
23
by: ShaneO | last post by:
Hello, I wish to extract embedded string data from a file using a Binary Read method. The following code sample is used in VB.NET and similar code is used in VB6 - (Assume variable...
11
by: Bob Rock | last post by:
Hello, I have an array of strings and need to find the matching one with the fastest possible code. I decided to order the array and then write a binary search algo. What I came up with is the...
4
by: Hunk | last post by:
Hi I have a binary file which contains records sorted by Identifiers which are strings. The Identifiers are stored in ascending order. I would have to write a routine to give the record given...
2
by: Timmy | last post by:
The bigger problem is with the Binary Search. The program crashes when it's excuted. and Visual Studio 2005 indicates stack over flow and shows a break at that function. Sequential search is...
0
by: Atos | last post by:
Binary search is used to locate a value in a sorted list of values. It selects the middle element in the array of sorted values, and compares it with the target value; that is the key we are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.