Sorting a Atring array

I have a String and i have to use a bubble sort it and i can't figure it out. i used the SUBSTRING to break the string in to words. here is my code so far any help would be greatly appreciated.publicclass stringArraySort{

publicstaticvoid main(String[] args){

String Pledge ="i pledge allegiance to the flag of the united states of america, and to the republic for which it stands: one nation under god, indivisible, with liberty and justice for all." ;

// String to be segmented

int count = 0;// Number of substrings

char separator =' ';// Substring separator

int j = 0 ;

String Swap =" " ;

// Determine the number of substrings

int index = 0;

do

{

++count;// Increment count of substrings

++index;// Move past last position

index = Pledge.indexOf(separator, index);

}

while (index != -1);

// Extract the substring into an array

String[] subStr =new String[count];// Allocate for substrings

index = 0;// Substring start index

int endIndex = 0;// Substring end index

for(int i = 0; i < count; i++)

{

endIndex = Pledge.indexOf(separator,index);// Find next separator

if(endIndex == -1)// If it is not found

subStr[i] = Pledge.substring(index);// extract to the end

else// otherwise

subStr[i] = Pledge.substring(index, endIndex);// to end index

index = endIndex + 1;// Set start for next cycle

}

//java.util.Arrays.sort(subStr);

// Display the substrings

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

System.out.println(subStr[i]);

// java.util.Arrays.sort(subStr);

for(j=0; j<subStr.length;j++){

if(subStr[j] == subStr[j+1]){

Swap = subStr[j];

subStr[j]=subStr[j+1];

subStr[j+1]=Swap;

}

}

}

}

>

[3562 byte] By [Shakejuhna] at [2007-11-26 20:48:41]
# 1

For starters you can make you code a lot cleaner by using the split method.

String sentence = "My hovercraft is full of eels";

String[] words = sentence.split(" ");

As for the rest of your code, you need to ask a specific question. As it stands your post reads as "Here is my code, fix it for me". Thats not how it works around here. You need to explain where you are having problems, post error messages, anything to make our job easier to help you.

floundera at 2007-7-10 2:12:02 > top of Java-index,Java Essentials,New To Java...
# 2
my question was on the sort, i cant get the bubble sort to work. i was reading and it says you need two nested loops but i don't quite understand this concept. i attempted it in the code above, and if any one could provide some hints in the right direction, thanks in advance.
Shakejuhna at 2007-7-10 2:12:02 > top of Java-index,Java Essentials,New To Java...
# 3
Search for bubble sort on Wikipedia. It will give a good explanation as well as code samples.
floundera at 2007-7-10 2:12:02 > top of Java-index,Java Essentials,New To Java...
# 4

try this.

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

{

for(int j=0; j><subStr.length-1-i; j++)

{

if(subStr[j] == subStr[j+1]) {

Swap = subStr[j];

subStr[j]=subStr[j+1];

subStr[j+1]=Swap;

}

}

>

fastmikea at 2007-7-10 2:12:02 > top of Java-index,Java Essentials,New To Java...
# 5
Supermike to the rescue. However what is the point of swapping two elements if and only if they are the same?
floundera at 2007-7-10 2:12:02 > top of Java-index,Java Essentials,New To Java...
# 6
mr flounder i thought he wanted to know how the bubble sort works. i did not even compile his code and did not read his code what he was trying to accomplish.
fastmikea at 2007-7-10 2:12:02 > top of Java-index,Java Essentials,New To Java...
# 7
thanks for all the tips guys, Reguards
Shakejuhna at 2007-7-10 2:12:02 > top of Java-index,Java Essentials,New To Java...
# 8
Yes, he wants to use bubble sort but the code you supplied will not work as pointed out. It will only swap elements if they are equal. What is the point of that?
floundera at 2007-7-10 2:12:02 > top of Java-index,Java Essentials,New To Java...