alphabetizing parameters

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

    alphabetizing parameters

    Hello,

    I've been placed on a web analytics project to try and get so me Java experience/web development. I have very little Java knowledge, and hoping that you guys can help me out a little.

    I have been asked to come up with a Java method that takes a URL string and alphabetizes the parameters. This is what i was given to start...

    Code:
    int questionPos = inUrl.indexOf("?");
                 if (questionPos >= 0) {
                  StringBuffer sb = new StringBuffer(inUrl.substring(0, questionPos + 1));
    
                  StringTokenizer tokenizer = new StringTokenizer(inUrl.substring(questionPos + 1), "&", true);
     
                while (tokenizer.hasMoreTokens()) {
                   String key = tokenizer.nextToken();
            }
     
         }

    I have no idea what this does or even where to start. Any help/guidance would be great!
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Stang02GT
    Hello,

    I've been placed on a web analytics project to try and get so me Java experience/web development. I have very little Java knowledge, and hoping that you guys can help me out a little.

    I have been asked to come up with a Java method that takes a URL string and alphabetizes the parameters. This is what i was given to start...

    Code:
    int questionPos = inUrl.indexOf("?");
                 if (questionPos >= 0) {
                  StringBuffer sb = new StringBuffer(inUrl.substring(0, questionPos + 1));
    
                  StringTokenizer tokenizer = new StringTokenizer(inUrl.substring(questionPos + 1), "&", true);
     
                while (tokenizer.hasMoreTokens()) {
                   String key = tokenizer.nextToken();
            }
     
         }

    I have no idea what this does or even where to start. Any help/guidance would be great!
    That code snippet tries to manipulate a URL: blablah?foo&bar &baz.
    It stores the blahblah? part in a StringBuffer and scans for the substrings that
    don't consist an ampersand '&'; in the example those are foo, bar and baz.

    I'd use the String.split() method if I were you,

    kind regards,

    Jos

    Comment

    • Stang02GT
      Recognized Expert Top Contributor
      • Jun 2007
      • 1206

      #3
      Alright thank you. Do you know of any examples i could look at of a the string method being used?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Stang02GT
        Alright thank you. Do you know of any examples i could look at of a the string method being used?
        You should read the API documentation for all those classes. Navigate to the
        Articles>Java section. There's an index article that contains links to those API
        documentation files. Download and bookmark them; if you're seriously into Java
        programming you'll need that documentation a lot.

        kind regards,

        Jos

        Comment

        • Stang02GT
          Recognized Expert Top Contributor
          • Jun 2007
          • 1206

          #5
          Alright thank you very much.


          I also consulted someone where i work and they suggested looking in to an ArrayList or a TreeSet.


          What do you think about those? Or should is the String.split still the best way ?

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Stang02GT
            Alright thank you very much.


            I also consulted someone where i work and they suggested looking in to an ArrayList or a TreeSet.


            What do you think about those? Or should is the String.split still the best way ?
            Your co-workers were talking about the sorting process itself: SortedSets and
            ArrayLists (that can easily be sorted too) are nice solutions to that little sub-problem
            but first you have to get those substrings out of that URL and the split method is
            just fine for that purpose.

            kind regards,

            Jos

            Comment

            • Stang02GT
              Recognized Expert Top Contributor
              • Jun 2007
              • 1206

              #7
              Alright sounds good. Thanks again!

              Comment

              • Stang02GT
                Recognized Expert Top Contributor
                • Jun 2007
                • 1206

                #8
                Once i have the link broken up into strings, what do you suggest is the best sorting method to complete my task of alphabetizing them?

                I have gotten a better understanding of what that code snippet does that i posted earlier( thanks to your help Jos ) and now i need to develope a way of sorting them which i have come to realize ( after i removed my head from my bottom end ) that my co-workers suggested that I look into using and ArrayList or TreeSet to accomplish the sort.

                Which of these do you think would work best? Or is there something else i should look into using?

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by Stang02GT
                  Once i have the link broken up into strings, what do you suggest is the best sorting method to complete my task of alphabetizing them?
                  It depends a bit where you put those String parts. Have a look at the Arrays.sort()
                  and the Collections.sor t() methods; both use the same sorting method so
                  there's no need to worry about that.

                  kind regards,

                  Jos

                  Comment

                  • Stang02GT
                    Recognized Expert Top Contributor
                    • Jun 2007
                    • 1206

                    #10
                    If I use .......


                    Code:
                    Collections.sort(list, string.CASE_INSENSITIVE_ORDER);
                    do you think this will work?

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by Stang02GT
                      If I use .......


                      Code:
                      Collections.sort(list, string.CASE_INSENSITIVE_ORDER);
                      do you think this will work?
                      Did you give it a try? I'd say yes that works if you capitalize the leading 's' in 'String'.

                      kind regards,

                      Jos

                      Comment

                      Working...