help with performing a binary search on a vector of strings

I am trying to create an application so that I can keep track of my baseball cards. I create a frame that has a text box and a JList. When the frame opens, the JList is populated with a list of baseball card brands pulled from a database. If a user types a letter into the textbox, then the first item in the list that matches that letter will be selected. So if a user types the letter d, then Donruss Bonus MVP?s will be selected.

I pulled the brands from the database and placed them into a Vector. From there I keep the Vector available so that I can use it while I am searching with the text from the text box.

When a user type?s text into the textbox, the text entered will be compared with the items in the Vector. I am trying to write a binary search to perform the searching. The problem that I am having is that the result of the search is always-1.

The search seems to never find the item I want it to look for. Can anyone help me with this problem?

Thanks Jason Franz.

Code and sample of the vector is located below.

privatevoid jTextField1_keyReleased(KeyEvent e){

jListReconciliation.setSelectedIndex(binarySearch(searchString, jTextField1.getText().trim(), 0, searchString.size() -1));

}

privateint binarySearch(Vector list, String text,int low,int high){

String temp ="";

if(low <= high){

int middle = ((low + high)/2);

temp = String.valueOf(list.elementAt(middle));

if(checkCharacters(temp, text)){

System.out.println(list.indexOf(list.elementAt(middle)));

return list.indexOf(list.elementAt(middle));

}elseif(String.valueOf(list.elementAt(middle)).compareToIgnoreCase(text) < 0){

System.out.println("LESS " + list.indexOf(list.elementAt(middle)));

return binarySearch(list, text, low, middle-1);

}else{

System.out.println("MORE " + list.indexOf(list.elementAt(middle)));

return binarySearch(list, text, middle-1, high);

}

}else{

return -1;

}

}

privateboolean checkCharacters(String main, String second){

boolean result =false;

for(int i = 0; i < second.length(); i++){

if(String.valueOf(main.charAt(i)).compareToIgnoreCase(String.valueOf(second.charAt(i))) == 0){

result =true;

}else{

returnfalse;

}

}

return result;

}

Below is a sample of the vector and of the code I am trying to write.

Baseball Stars Series 1

Baseball Stars Series 2

Circa Thunder Rave Reviews

Circa Thunder Super Rave

Classic

Donruss Bonus MVP's

Donruss Coke Ryan

Embossed

Embossed Golden Idols

Fleer Baseball MVP

Fleer Sunoco

Metal Universe Platinum Portraits

Metal Universe Precious Metal Gems

Metal Universe Titanium

Nabisco All-Star Autographs

National Convention Promo

National Packtime

Pacific Silver

Pinnacle Aficionado Slick Picks

Score Airmail

Sportflix Artist's Proofs

Stadium Club Souvenir

Topps Chrome Hall Bound Refractor

Triple Play Gallary of Stars

Ultra Hockey

Upper Deck Future Stock Prospect

Woolworth's

Zenith

[4868 byte] By [jasonfranz] at [2007-9-30 19:26:23]
# 1
I'd advise:1) Separate your application logic from the GUI. It's much easier to develop and maintain your code that way.2) Use the binary search provided by java.util.Collections. Don't reinvent the wheel.3) Don't use Vector, use ArrayList or some other list.
paulcw at 2007-7-6 23:39:52 > top of Java-index,Java Essentials,Java Programming...