Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

A problem with a java exercise

Options
  • 08-02-2006 2:50pm
    #1
    Registered Users Posts: 871 ✭✭✭


    Here is the exercise

    "In this weeks practice exercise you should write a class which takes an array of Strings from the user at the command prompt (rather than from a series of JOp tionPanes). After reading in the Strings from the user, your class should sort the array in to ascending order alphabetically and then display the Strings to the user in sorted order. Recall the worked example of selection sort done in lectures last Thursday. Your class should print out the strings in the array on the command prompt, with a space between each string.

    For example, suppose your class is called MySortProgram. It should produce the following output:

    >java MySortProgram fintan alex zebedee pat mike
    alex fintan mike pat zebedee

    Remember that the list of strings given here after the program name appears in the array of strings called args in the main method of your class."

    I have no idea where to even start, can anyone help at all?


Comments

  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    gerry87 wrote:
    I have no idea where to even start, can anyone help at all?

    Is it too late to change classes?


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    You sort on the 1st character, where two strings have the same comparison character you'll have to compare them on subsequent characters.

    Java probably has functions to do this, I'm no Java head. But you should be able to do it nonetheless.

    C:/>java MySortProg alexandra alexander liam jason carol paula william paul mark
    alexander alexandra carol jason liam mark paul paula william


  • Registered Users Posts: 4,188 ✭✭✭pH


    import java.util.*;
    import java.io.*;
    
    public class TestSort1 {
    
    public static void main(String args[]) {
          java.util.Arrays.sort(args);
          for(int i=0; i < args.length ; i++) 
               System.out.print(args[i] + " ");
     }
          
    }
    
    Now I feel dead smart!


  • Registered Users Posts: 16,732 ✭✭✭✭Nalz


          java.util.Arrays.sort(args);
    

    Java -I love it, the handy way out!!

    is there not something he has to do with the args, like checking argv and argc? Can remember doin something like this but java isnt my strong point


  • Closed Accounts Posts: 521 ✭✭✭EOA_Mushy


    pH wrote:
    import java.util.*;
    import java.io.*;
    
    public class TestSort1 {
    
    public static void main(String args[]) {
          java.util.Arrays.sort(args);
          for(int i=0; i < args.length ; i++) 
               System.out.print(args[i] + " ");
     }
          
    }
    
    Now I feel dead smart!

    The OP will most likly get shot for this.... (Too good/simple)
    Probably starting in college, ment to learn sorting algorithms...


  • Advertisement
  • Closed Accounts Posts: 888 ✭✭✭themole


    hmmm, seing as todays lab was from 4-6 you probably have this done already.

    if not, there is another lab tomorrow from from 2-4.

    the solution given does work, but it is not what fintan wanted.

    he explained the example in the lecture last thursday and a layout of selection sort can be found in the note on arrays.
    is there not something he has to do with the args, like checking argv and argc?

    length of args is args.length

    if the length is not checked the example given just prints out nothing


  • Closed Accounts Posts: 888 ✭✭✭themole


    EOA_Mushy wrote:
    The OP will most likly get shot for this.... (Too good/simple)
    Probably starting in college, ment to learn sorting algorithms...

    ya.

    pretty much.

    1st Year Java UCD


Advertisement