Sorting variables from the URL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stang02GT
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    Sorting variables from the URL

    Hello,

    I posted something about this a couple weeks ago and was pointed into the right direction, but lack of time with school and other projects at work, i haven't been able to sit down and figure out how to accomplish this task and the code that i need to do it.

    Here is my situation.

    I have been asked to attempt to develop code that will alphabetize parameters after the input has been broken up by this code(which was given to me).....

    [CODE=java]int questionPos = inUrl.indexOf(" ?");
    if (questionPos >= 0) {
    StringBuffer sb = new StringBuffer(in Url.substring(0 , questionPos + 1));

    StringTokenizer tokenizer = new StringTokenizer (inUrl.substrin g(questionPos + 1), "&", true);

    while (tokenizer.hasM oreTokens()) {
    String key = tokenizer.nextT oken();
    }

    }[/CODE]

    In my searching and suggestions I have received, doing this could be done by

    Arrays.sort()
    or
    Collections.sor t()

    My issue is I do not know where/how to begin developing code for this. I just looked at Java/Java script for the 1st time a couple weeks ago, so I am completely clueless.

    Here is an example of the input

    [CODE=html]Ex. input: <a href="/ffw/funds/divcapgains.do? year=2007&order by=data_1&order key=DESC">Feder ated Fund Name</a>[/CODE]


    If someone could help me develop code for this so that i can begin to get my feet wet and have something to build off of i would greatly appreciate it.
    Last edited by pbmods; Aug 28 '07, 12:59 AM. Reason: Changed [CODE] to [CODE=html] and [CODE=java].
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Stang.

    Changed thread title to better describe the problem (did you know that threads whose titles that do not follow the Posting Guidelines actually get FEWER responses?).

    Comment

    • Logician
      New Member
      • Feb 2007
      • 210

      #3
      Originally posted by Stang02GT
      I just looked at Java/Java script for the 1st time a couple weeks ago, so I am completely clueless.
      Clue: Those are two different languages and this forum doesn't deal with the one you're being asked to use.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by Logician
        Clue: Those are two different languages and this forum doesn't deal with the one you're being asked to use.
        Moved to the Java forum where it belongs.

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          Originally posted by Stang02GT
          I have been asked to attempt to develop code that will alphabetize parameters after the input has been broken up by this code(which was given to me).....

          [CODE=java]int questionPos = inUrl.indexOf(" ?");
          if (questionPos >= 0) {
          StringBuffer sb = new StringBuffer(in Url.substring(0 , questionPos + 1));

          StringTokenizer tokenizer = new StringTokenizer (inUrl.substrin g(questionPos + 1), "&", true);

          while (tokenizer.hasM oreTokens()) {
          String key = tokenizer.nextT oken();
          }

          }[/CODE]

          In my searching and suggestions I have received, doing this could be done by

          Arrays.sort()
          or
          Collections.sor t()

          My issue is I do not know where/how to begin developing code for this. I just looked at Java/Java script for the 1st time a couple weeks ago, so I am completely clueless.

          Here is an example of the input

          [CODE=html]Ex. input: <a href="/ffw/funds/divcapgains.do? year=2007&order by=data_1&order key=DESC">Feder ated Fund Name</a>[/CODE]
          OK, first of all you will need a main method.
          Your basic class will look like this:
          [CODE=java]
          public class AlphabetizePara meters
          {
          public static void main(String[] args)
          {

          }
          }
          [/CODE]
          Into that, you insert the code, which you were given:
          [CODE=java]
          import java.util.*;

          public class AlphabetizePara meters
          {
          public static void main(String[] args)
          {
          int questionPos = inUrl.indexOf(" ?");
          if (questionPos >= 0)
          {
          StringBuffer sb = new StringBuffer(in Url.substring(0 , questionPos + 1));
          StringTokenizer tokenizer = new StringTokenizer (inUrl.substrin g(questionPos + 1), "&", true);
          sorted = new String[tokenizer.count Tokens()/2+1];
          while (tokenizer.hasM oreTokens())
          {
          String key = tokenizer.nextT oken();
          }
          }
          }
          }
          [/CODE]
          I don't know, how the URL should be given to the function, but let's just assume, that it's given to the program as an arguemnt (so when you call it, it's something like "java AlphabetizePara meters /ffw/funds/divcapgains.do? year=2007&order by=data_1&order key=DESC"). Now you can access that as args[0].

          You will need an Array (or whatever Container you want to use) to put the parameters into, so create one:
          [CODE=java]
          String[] parameters;
          ...
          parameters = new String[amountOfParamet ers];
          [/CODE]
          Then you have to put the parameters into the Array ([i]parameters = whateverStringI tShouldBe;).
          As soon as you've done that, you can sort your Array with Arrays.sort(par ameters);

          One last hint: You can't display an array of Strings with System.out.prin tln(parameters) ;, however you can do this:
          [CODE=java]
          for(int i=0;i<parameter s.length();i++)
          System.out.prin tln(parameters[i]);
          [/CODE]

          Comment

          • Stang02GT
            Recognized Expert Top Contributor
            • Jun 2007
            • 1206

            #6
            Originally posted by nepomuk
            OK, first of all you will need a main method.
            Your basic class will look like this:
            [CODE=java]
            public class AlphabetizePara meters
            {
            public static void main(String[] args)
            {

            }
            }
            [/CODE]
            Into that, you insert the code, which you were given:
            [CODE=java]
            import java.util.*;

            public class AlphabetizePara meters
            {
            public static void main(String[] args)
            {
            int questionPos = inUrl.indexOf(" ?");
            if (questionPos >= 0)
            {
            StringBuffer sb = new StringBuffer(in Url.substring(0 , questionPos + 1));
            StringTokenizer tokenizer = new StringTokenizer (inUrl.substrin g(questionPos + 1), "&", true);
            sorted = new String[tokenizer.count Tokens()/2+1];
            while (tokenizer.hasM oreTokens())
            {
            String key = tokenizer.nextT oken();
            }
            }
            }
            }
            [/CODE]
            I don't know, how the URL should be given to the function, but let's just assume, that it's given to the program as an arguemnt (so when you call it, it's something like "java AlphabetizePara meters /ffw/funds/divcapgains.do? year=2007&order by=data_1&order key=DESC"). Now you can access that as args[0].

            You will need an Array (or whatever Container you want to use) to put the parameters into, so create one:
            [CODE=java]
            String[] parameters;
            ...
            parameters = new String[amountOfParamet ers];
            [/CODE]
            Then you have to put the parameters into the Array ([i]parameters = whateverStringI tShouldBe;).
            As soon as you've done that, you can sort your Array with Arrays.sort(par ameters);

            One last hint: You can't display an array of Strings with System.out.prin tln(parameters) ;, however you can do this:
            [CODE=java]
            for(int i=0;i<parameter s.length();i++)
            System.out.prin tln(parameters[i]);
            [/CODE]

            Thank you very much!

            Comment

            • Stang02GT
              Recognized Expert Top Contributor
              • Jun 2007
              • 1206

              #7
              You will need an Array (or whatever Container you want to use) to put the parameters into, so create one:
              [CODE=java]
              String[] parameters;
              ...
              parameters = new String[amountOfParamet ers];
              [/CODE]
              Then you have to put the parameters into the Array ([i]parameters = whateverStringI tShouldBe;).
              As soon as you've done that, you can sort your Array with Arrays.sort(par ameters);

              One last hint: You can't display an array of Strings with System.out.prin tln(parameters) ;, however you can do this:
              [CODE=java]
              for(int i=0;i<parameter s.length();i++)
              System.out.prin tln(parameters[i]);
              [/CODE]

              nepomuk,

              First thank you very much for helping me. I apologize for my ignorance and lack of knowledge with Java. In the section above I would like to use an Array/ArrayList to do the sorting. This is now where i am getting confused, I follow what you are saying, but do to my lack of knowledge in this area.

              [CODE=java]
              String[] parameters;
              ...
              parameters = new String[amountOfParamet ers];
              [/CODE]

              I'm not sure what the above code should be doing, and what i need to add into the (...) part?

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                You can display an array of Strings (or any other objects) using
                [CODE=java]Arrays.toString (array);[/CODE]
                See the details in the docs for the Arrays class.

                Comment

                • Nepomuk
                  Recognized Expert Specialist
                  • Aug 2007
                  • 3111

                  #9
                  Originally posted by Stang02GT
                  nepomuk,

                  First thank you very much for helping me. I apologize for my ignorance and lack of knowledge with Java. In the section above I would like to use an Array/ArrayList to do the sorting. This is now where i am getting confused, I follow what you are saying, but do to my lack of knowledge in this area.

                  [CODE=java]
                  String[] parameters;
                  ...
                  parameters = new String[amountOfParamet ers];
                  [/CODE]

                  I'm not sure what the above code should be doing, and what i need to add into the (...) part?
                  The first line (String[] parameters;) defines an Array of Strings. At this point, the Array is not existent yet.

                  The last line (parameters = new String[amountOfParamet ers];) actually creates the Array. The Strings however still must be defined.

                  Inbetween both lines you have to find out, how many Elements (Strings) the Array will have.

                  After that, you'll have to give the Elements of the Array values.
                  Originally posted by r035198x
                  You can display an array of Strings (or any other objects) using
                  [CODE=java]
                  Arrays.toString (array);[/CODE]
                  See the details in the docs for the Arrays class.
                  That is very usefull, thank you!

                  Comment

                  • Stang02GT
                    Recognized Expert Top Contributor
                    • Jun 2007
                    • 1206

                    #10
                    Thanks again for all of your help!

                    Comment

                    Working...