lol, am new to java, new to programming and getting used to logical thinking.
i've been working thru the examples in the book, am on arrays & strings, 3rd question. it asks to take a string variable apart and break words into alphabetical order. i did it one way, he does it another, which is better, mine is shorter and works with his text. include any punctuation marks in his code and it falls over.
heres my code:-
public class TextSort2 {
public static void main(String[] args) {
// declare & initilize arrays & variables
String text = "Into the face of the young man who sat on the terrace " +
"of the Hotel Magnifique at Cannes crept a look of furtive " +
"shame, the shifty, hangdog look which announces that " +
"an Englishman is about to talk French.";
String delimiters = "[' ,.!]";
// analyse the string & break into words
String[] words = text.split(delimiters, 0);
System.out.println("The text string nice and raw!\n\n" + text);
// display string as it is in table form
System.out.println("The text form in array form:\n\n");
for(String word : words) {
System.out.println(word);
}
// sort words into alphabetical order
int i=0; //element index
String tempWord; // initize and declare tempWord
while(i < (words.length-1)) { // block of code executes
// is element[i] in words greater than element[i+1] ?
if(words[i].compareTo(words[i+1]) > 0) {
// if yes swap element i with i+1 by using tempWord
tempWord = words[i]; // store words[i] in temp
words[i] = words[i+1]; // set words[i] to value of i+1
words[i+1] = tempWord;
i = 0; // reset counter and go back to beg
} else {
// otherwise increment index and repeat loop
i++;
}
}
// Display results
System.out.println("\n\nThe text broken into individual elements\n" +
"and sorted alphabeticaly:\n\n");
for(String word : words) {
System.out.println(word);
}
}
}
HERES HIS:
__________________________________________________ ___________________
//Chapter 4, Exercise 3
// This solution scans the string character by character to extract words.
// We cannot conveniently use a StringTokenizer object or the indexOf() method
// in the String class because a word can end with a space or a punctuation character
// such as a period or a comma. For simplicity, we assume a single quote
// is always part of a word.
public class Sort {
public static void main(String args[]) {
// Declare the String that is to be sorted:
String text = "Into the face of the young man who sat on the terrace " +
"of the Hotel Magnifique at Cannes crept a look of furtive " +
"shame, the shifty, hangdog look which announces that " +
"an Englishman is about to talk French." ;
// Determine how many words there are.
// A word is a sequence of letters that may include a single quote character.
// Anything else is punctuation or spaces.
int count = 0; // Counts number of words
boolean isWord = false; // Indicates start of a word found
for (int i = 0 ; i<text.length() ; i++) {
if(isWord) { // If we have found a word...
if(Character.isLetter(text.charAt(i)) || text.charAt(i) == '\'')
continue; // ...pass over letters or single quote
else // It is not a letter or single quote...
isWord = false; // so we have come to the end of the word
}
else if(Character.isLetter(text.charAt(i))) {
// We have the first letter of a new word
count++; // so increment word count
isWord = true; // and record we have found a word
}
}
// Create the array of strings to contain all the words:
String[] words = new String[count];
// Now extract the words from the text
int start = 0; // Position of first letter of a word
int wordIndex = 0; // Current vacant words array element
isWord = false; // Indicates when a word start is found
for (int i = 0 ; i<text.length() ; i++) {
if(!isWord) { // If we are not in a word...
if(Character.isLetter(text.charAt(i))) {// look for 1st lett of a wo
start = i; // Record word start index
isWord = true; // We have a word
}
}
else { // We are in a word
if(Character.isLetter(text.charAt(i)) || text.charAt(i) == '\'')
continue; // Still the same word so continue
else { // It is the end of the word
isWord = false; // so reset word indicator
words[wordIndex++] = text.substring(start,i);
} // and extract the word
}
}
// If the text ends with a letter, we will not have stored the last word
if(wordIndex < words.length)
words[wordIndex] = text.substring(start);
// Sort the array of words
String temp = null; // Stores a word reference
boolean exchange = true; // Indicate we exchanged a pair of words
while(exchange) {
exchange = false; // No exchange so far
for(int i = 1 ; i<words.length ; i++) {// For each word starting
if(words[i-1].compareTo(words[i])>0) {// with the nd If the previ
temp = words[i]; // word is greater exchange them
words[i] = words[i-1];
words[i-1] = temp;
exchange = true; // and record that exchange occurred
}
}
}
// Display the sorted array of words:
for(String word : words) {
System.out.println(word);
}
}
}
anybody got any thoughts as they would be appreciated!
dreD
