(no title) (original) (raw)

I'm really sorry to do this. I've tried my best to do all my code by myself without help, and for the most part it's been going very well in my class. Now, unfortunately, I've hit a roadblock and I can't figure out what's wrong with the code. I apologize, I know how frustrating it must be to the experts here when the people who are new to Java make idiodic mistakes. That being said, I hope someone can help me here.

The homework assignment was to get a number of .txt files, load them up into vectors, and then sort them alphabetically, among other things. It's the sorting alphabetically that I'm having problems with. The method I wrote up, to the best of my knowledge, should have taken the first word's first letter and compared it to the second word's first letter. Unfortunately, what comes out is a few words repeating constantly. Any help is very much appreciated. Thank you very much.

public void sortVector(Vector v) { String wd1, wd2; Object temp; int wdVal; for (int i = 0; i < (v.size() + 1 ); i++) { for (int j = i + 1; j < v.size(); j++) { wd1 = (String) (v.elementAt(i)); wd2 = (String) (v.elementAt(j)); wdVal = wd1.compareTo(wd2);
if (wdVal < 0) { temp = v.elementAt(i); v.setElementAt(temp, j); } } } }