Wrox Programmer Forums
|
BOOK: Ivor Horton's Beginning Java, Java 7 Edition
This is the forum to discuss the Wrox book Ivor Horton's Beginning Java, Java 7 Edition by Ivor Horton ; ISBN: 978-0-470-40414-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Java, Java 7 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 December 21st, 2011, 03:07 AM
Registered User
 
Join Date: Dec 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default Need help with Ch4 Exercise 3!

The code below. Why won't my program sort the words. I can't figure out whats wrong with my code.

Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author shawn.lim.2010
 */
public class Ch4Ex3 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String text = "This is my new program";
        String delimiters = "[ ]";


        
        // Analyze the string
        String[] tokens = text.split(delimiters, 0);
        String[] sorted = new String[tokens.length];
        
        for (String token : tokens){
            System.out.println(token);                       
        }

        for (int i = 0; i < (tokens.length-1); ++i){
            int result = (tokens[i]).compareTo(tokens[i+1]);
            if (result > 0){
                sorted[i]=tokens[i+1];
                sorted[i+1]=tokens[i];
                        
            } else {
                sorted[i] = tokens[i];
                sorted[i+1] = tokens[i+1];

                        
            }
            
        }
                    System.out.println("\nThe sorted list of words is now: \n");
        for (String sort : sorted){

            System.out.println(sort);
        }
    }
}
 
Old December 22nd, 2011, 02:37 PM
Registered User
 
Join Date: Dec 2011
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
Default

I finally got to this exercise (took me long enough). Here's what I think you need to change:

1) I think you should use just one array. No need for both the sorted and tokens array. Just the tokens array should be fine.

2) You'll have to do more than one pass over the array to completely sort it. At the moment the code above just does one pass. Lets say tokens[tokens.length-1] is the smallest string. So after sorting it's value should be in tokens[0]. After the first pass it will get moved to tokens[tokens.length-2]. Then in the second pass it'll move to tokens[tokens.length-3] and so on until the value finally reaches tokens[0]. You can see you'll have to do at most tokens.length passes in the worst case.

Hope this helps.
The Following User Says Thank You to JammingKhan For This Useful Post:
xiao_mao_mimi (December 26th, 2011)





Similar Threads
Thread Thread Starter Forum Replies Last Post
VB: Ch4 p132 Security Try It Out Rich57 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 October 19th, 2007 05:05 AM
Ch4 Problem daivos03 JSP Basics 9 January 30th, 2006 01:57 AM
Problems in Ch3 and Ch4 Maram BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 2 August 4th, 2005 09:28 AM
ch4 wrong output? pete12345 JSP Basics 3 January 24th, 2004 09:51 AM





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