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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

A problem with a java exercise

  • 08-02-2006 1:50pm
    #1
    Registered Users, Registered Users 2 Posts: 872 ✭✭✭


    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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 16,898 ✭✭✭✭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