I have been working on a dictionary project for school for hours now. All i need is a second pair of eyes as i have been staring at this to long. The solution is probably right in front of my face, but here is my code:
and here is the silly method i have used to test out the function, my main statement is to messy and unorganized to post:
It returns -1 for any string i try, and if doesn't return -1 it gets stuck in an infinite loop, or the index is wrong. I will continue to search for the solution, but i figured it wouldn't hurt to get another pair of eyes or two to look it over. Thank You in advance!
P.S I am a first year in computer science at my college, so go easy on me! I'm fairly new to programming.
public static int binarySearchWord(Word [] a, String key){
selSortWords(a);
int low, high;
low = 0;
high = a.length -1;
while(high>low){
int mid = (low + high) /2;
if((a[mid].word).compareTo(key) < 0){
high = mid +1;
}
else if((a[mid].word).compareTo(key) > 0){
low = mid -1;
}
else{
return mid;
}
}
return -1;
}
and here is the silly method i have used to test out the function, my main statement is to messy and unorganized to post:
static void STUFF(String a){
System.out.println(binarySearchWord(lol(),"zabv"));
}
static Word[] lol(){
Word[] a = new Word[5];
a[0] = new Word("zabv",0);
a[1] = new Word("bsadf",1);
a[2] = new Word("csadf",2);
a[3] = new Word("dasdf",3);
a[4] = new Word("adasdf",4);
return a;
}
It returns -1 for any string i try, and if doesn't return -1 it gets stuck in an infinite loop, or the index is wrong. I will continue to search for the solution, but i figured it wouldn't hurt to get another pair of eyes or two to look it over. Thank You in advance!
P.S I am a first year in computer science at my college, so go easy on me! I'm fairly new to programming.