Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: where to break array sorting?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default where to break array sorting?

    For an assignment ive had to have a problem read in a string and then display the string and state if the letters in the string are ascending or not. I've managed to do this however i swear no matter where i add either

    if ( input.equals( "END" ){
    break;
    }

    or a

    while ( ! input.equals( "END" ){
    break;
    }

    it won't break after i receive "letters are not in ascending order"

    Heres the code with the simplistic ifs

    class Main
    {
        public static void main( String args[] )
        {
            System.out.print("#Enter Word Here:");
            String input = BIO.getString();
     
            char[] characters = input.toCharArray();
     
            boolean haveSwapped = true;          
     
            while ( haveSwapped )               
            {
            if ( input.equals( "END") ){
                 break;
            }
                haveSwapped = false;              
                for (int i=0; i<characters.length-1; i++)
                {                                  
                    if ( characters[i] > characters[i+1] ) 
                    {
                        char tmp = characters[i];         
                        characters[i] = characters[i+1];
                        characters[i+1] = tmp;
                        haveSwapped = true;       
                        System.out.print( input );
                        System.out.println(" letters are not in ascending order");
                        System.out.print("#Enter Word Here:");
                        input = BIO.getString();
                        if ( input.equals( "END") ){
                            break;
                        }
                        characters = input.toCharArray();
                        i = 0;
                    }
                }  
                System.out.print( input );
                System.out.println(" letters are in ascending order");
                System.out.print("#Enter Word Here:");
                input = BIO.getString();
                characters = input.toCharArray();
                haveSwapped = true;
                if ( input.equals( "END") ){
                    break;
                }
            }
        }
    }

    Is it possible? where and how? :S thanks for any help.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: where to break array sorting?

    To see what the code sees, add a prinln to print out the value of the input variable immediately after you read it in. Be sure to add delimiting Strings: println("input=" + input + "<") so you can see if input is empty or has extra spaces in it.

  3. #3
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: where to break array sorting?

    You have a for loop inside a while loop. Your break statement would cause execution to break only from the immediate loop (the for loop). Control will be received by the while loop at the point following the for loop. Is this as you intend?

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: where to break array sorting?

    Quote Originally Posted by 2by4 View Post
    You have a for loop inside a while loop. Your break statement would cause execution to break only from the immediate loop (the for loop). Control will be received by the while loop at the point following the for loop. Is this as you intend?
    3

    No, i intended the program to end overall. :S

  5. #5
    Member
    Join Date
    Dec 2011
    Posts
    48
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: where to break array sorting?

    Quote Originally Posted by BITmixit View Post
    3

    No, i intended the program to end overall. :S
    The java language allows you to use a label to specify the construct from which execution would break. This is necessary if you wish to break directly out of an outer loop from within a nested loop. You may wish to reference your favourite java language manual for how to use labels. :-)

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: where to break array sorting?

    Right, got it working it now exits upon entry of END. However when trying to submit it to the system which checks our code online, compiles it and runs examples mine doesn't work. Heres the code:

    class Main
    {
        public static void main( String args[] )
        {
            System.out.print("#Enter Word Here:");
            String input = BIO.getString();
     
            char[] characters = input.toCharArray();
     
            boolean haveSwapped = true;          
     
            while ( haveSwapped )               
            {
                if ( input.equals( "END" ) ){
                    System.exit(0);
                }
                haveSwapped = false;              
                for (int i=0; i<characters.length-1; i++)
                {                                  
                    if ( characters[i] > characters[i+1] ) 
                    {
                        char tmp = characters[i];         
                        characters[i] = characters[i+1];
                        characters[i+1] = tmp;
                        haveSwapped = true;       
                        System.out.printf("%-10s letters not in ascending order", input);
                        System.out.println();
                        System.out.print("#Enter Word Here:");
                        input = BIO.getString();
                        if ( input.equals( "END") ){
                            System.exit(0);
                        }
                        characters = input.toCharArray();
                        i = 0;
                    }
                }  
                System.out.printf("%-10s letters are in ascending order", input);
                System.out.println();
                System.out.print("#Enter Word Here:");
                input = BIO.getString();
                if ( input.equals( "END" ) ){
                    System.exit(0);
                }
                characters = input.toCharArray();
                haveSwapped = true;
            }
        }
    }

    Heres the output from the testing system.

    ----------Data used as input was----------------------------------------
    abcdefgh
    Xabcdefg
    aXbcdefg
    abXcdefg
    abcXdefg
    abcdXefg
    abcdeXfg
    abcdefXg
    abcdefgX
    ABCDEFGH
    END
    ----------Expected answer was-------------------------------------------
    abcdefgh letters in ascending order
    Xabcdefg letters not in ascending order
    aXbcdefg letters not in ascending order
    abXcdefg letters not in ascending order
    abcXdefg letters not in ascending order
    abcdXefg letters not in ascending order
    abcdeXfg letters not in ascending order
    abcdefXg letters not in ascending order
    abcdefgX letters in ascending order
    ABCDEFGH letters in ascending order
    ----------Your answer however was [ Excluding lines containing a # ] ---
    abcdefgh letters in ascending order
    Xabcdefg letters in ascending order
    aXbcdefg letters not in ascending order
    abXcdefg letters not in ascending order
    abcXdefg letters not in ascending order
    abcdXefg letters not in ascending order
    abcdeXfg letters not in ascending order
    abcdefXg letters not in ascending order
    abcdefgX letters not in ascending order
    ABCDEFGH letters in ascending order

    I dont really understand why abcdefgX and Xabcdefg aren't doing what they are supposed do.

    Any help?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: where to break array sorting?

    Xabcdefg letters not in ascending order
    I'm confused why those letters are not in ascending order. The ASCII values for the letters are:
    X= \u0058
    a= \u0061
    b= \u0062

    Based on that X comes before a which comes before b.

    What do the sorting rules say for ordering the letters? Are you supposed to ignore case?
    Which comes first A or a?

  8. #8
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: where to break array sorting?

    I assumed it was a, A, b, B, c, C

    My friends seems to think char[] characters = input.toCharArray();

    is converting any capitals from a string into normal letters before adding to an array.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: where to break array sorting?

    You definitely need the correct sorting sequence before you can write the code.
    Do you understand my last post about 'X' being lower in value than 'a'?
    That is what the ASCII values are.
    If you want to change that ordering, you will need special code to handle uppercase letters.

    Definite the order that you want, then you can write the code.

  10. #10
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: where to break array sorting?

    No i dont really understand it :S

    I want the order to be like i stated:

    A, a, B, b, C, c

    How do i go about changing the order?

    Is there no way i can tell the sorting process to ignore capitals or treat capitals as normals?
    Last edited by BITmixit; December 13th, 2011 at 07:52 AM.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: where to break array sorting?

    Your code will have to look at the case of each letter and consider that when comparing them.
    Your code has a simple comparison:
     if ( characters[i] > characters[i+1] )
    This will have to be changed to consider the case of the letter.
    One way will be to change both of the letters to be the same case before comparing them.
    If the same case letters match then you need to test if the original versions are the same case or different case.

  12. #12
    Junior Member
    Join Date
    Oct 2011
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: where to break array sorting?

    Finished it and it works. I basically just told the program to convert the string to lowercase before putting it in order and then just simply printed the original string so it still looked like capitals.

    Thanks for the help completed 2 assignments in the space of an hour thanks to this

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: where to break array sorting?

    Did you try these two Strings to see which is in order and which is not:
    Xx
    xX

    The testing you describe would fail with these.

Similar Threads

  1. Array sorting
    By renars in forum Java Theory & Questions
    Replies: 4
    Last Post: May 17th, 2011, 10:45 AM
  2. Sorting an array
    By thebestpearl in forum Collections and Generics
    Replies: 5
    Last Post: April 17th, 2011, 08:58 PM
  3. [SOLVED] Sorting an Array
    By petemyster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 12th, 2010, 11:07 AM
  4. array sorting
    By herbisey in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 27th, 2010, 12:07 PM
  5. Sorting an Array
    By Prince_85 in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 21st, 2010, 03:00 PM