Created
August 16, 2012 22:44
-
-
Save els-pnw/3374292 to your computer and use it in GitHub Desktop.
the program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
How many strings would you like to enter? 5 | |
Enter string 1: Lucy | |
Enter string 2: Kyzicke | |
Enter string 3: Lazy | |
Enter string 4: Legion | |
Enter string 5: blah | |
Lucy Kyzicke Lazy Legion blah |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class SortStrings { | |
/** | |
* We'll do a basic bubble sort,. | |
* @param array | |
* @param n | |
*/ | |
public static void MySort(String array[], int n) { | |
String tempWord; | |
for(int i = 0; i < n; i++) { | |
for(int j = 1; j < (n-i); j++) { | |
if (array[j-1].compareTo(array[j]) > 1) { | |
tempWord = array[j-1]; | |
array[j-1] = array[j]; | |
array[j] = tempWord; | |
} | |
} | |
} | |
} | |
/** | |
* Our main will prompt for number strings the user will enter | |
* then prompt for the strings. | |
* @param argv | |
* @throws IOException | |
*/ | |
public static void main(String [] argv) throws IOException { | |
BufferedReader stdin = | |
new BufferedReader(new InputStreamReader(System.in), 1); | |
// Lets determine the number of strings we'll sort | |
System.out.print("How many strings would you like to enter? "); | |
// I know, it's an odd place to do so, but we're going to | |
// init some variables here. | |
int numberOf = Integer.parseInt(stdin.readLine()); | |
String[] sortableStrings = new String[numberOf]; | |
// We're building an array now... | |
for (int i = 0; i < numberOf; i++) { | |
int x = i + 1; | |
System.out.print("Enter string " + x + ": "); | |
String s = stdin.readLine(); | |
sortableStrings[i] = s; | |
} | |
// The array is built, lets call our sort logic | |
// and return the sorted output. | |
MySort(sortableStrings, sortableStrings.length); | |
for(int i = 0; i < sortableStrings.length; i++) { | |
System.out.print(sortableStrings[i] + " "); | |
} | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment