Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Beginning Java 2, JDK 5 Edition
|
BOOK: Beginning Java 2, JDK 5 Edition
This is the forum to discuss the Wrox book Ivor Horton's Beginning Java 2, JDK 5 Edition by Ivor Horton; ISBN: 9780764568749
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2, JDK 5 Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 30th, 2005, 03:12 PM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to dredman367
Default beg java 1.5 = different example solutions ideas!

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
 
Old March 30th, 2005, 03:29 PM
Registered User
 
Join Date: Mar 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to dredman367
Default

apologies, jis code doesnt fall over, must have been a typo when inserting new text into his code.

thoughts would still be appreciated though!


dreD





Similar Threads
Thread Thread Starter Forum Replies Last Post
Get here your complete solutions in Java! Thomos Java Basics 1 August 22nd, 2007 10:27 AM
Java exercises and solutions uddessjava Java Basics 1 March 20th, 2006 11:36 PM
Source Code Beg. Java 2 colombo_ar BOOK: Beginning Java 2 3 August 28th, 2004 05:27 AM
Download code Beg Java Databases HERE severin Java Databases 0 April 18th, 2004 11:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.