Connecting Tech Pros Worldwide Forums | Help | Site Map

alphabetizing parameters

Stang02GT's Avatar
Moderator
 
Join Date: Jun 2007
Location: USA
Posts: 1,152
#1: Aug 14 '07
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...

Expand|Select|Wrap|Line Numbers
  1. int questionPos = inUrl.indexOf("?");
  2.              if (questionPos >= 0) {
  3.               StringBuffer sb = new StringBuffer(inUrl.substring(0, questionPos + 1));
  4.  
  5.               StringTokenizer tokenizer = new StringTokenizer(inUrl.substring(questionPos + 1), "&", true);
  6.  
  7.             while (tokenizer.hasMoreTokens()) {
  8.                String key = tokenizer.nextToken();
  9.         }
  10.  
  11.      }

I have no idea what this does or even where to start. Any help/guidance would be great!

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Aug 14 '07

re: alphabetizing parameters


Quote:

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...

Expand|Select|Wrap|Line Numbers
  1. int questionPos = inUrl.indexOf("?");
  2.              if (questionPos >= 0) {
  3.               StringBuffer sb = new StringBuffer(inUrl.substring(0, questionPos + 1));
  4.  
  5.               StringTokenizer tokenizer = new StringTokenizer(inUrl.substring(questionPos + 1), "&", true);
  6.  
  7.             while (tokenizer.hasMoreTokens()) {
  8.                String key = tokenizer.nextToken();
  9.         }
  10.  
  11.      }

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
Stang02GT's Avatar
Moderator
 
Join Date: Jun 2007
Location: USA
Posts: 1,152
#3: Aug 14 '07

re: alphabetizing parameters


Alright thank you. Do you know of any examples i could look at of a the string method being used?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Aug 14 '07

re: alphabetizing parameters


Quote:

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
Stang02GT's Avatar
Moderator
 
Join Date: Jun 2007
Location: USA
Posts: 1,152
#5: Aug 14 '07

re: alphabetizing parameters


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 ?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Aug 14 '07

re: alphabetizing parameters


Quote:

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
Stang02GT's Avatar
Moderator
 
Join Date: Jun 2007
Location: USA
Posts: 1,152
#7: Aug 14 '07

re: alphabetizing parameters


Alright sounds good. Thanks again!
Stang02GT's Avatar
Moderator
 
Join Date: Jun 2007
Location: USA
Posts: 1,152
#8: Aug 15 '07

re: alphabetizing parameters


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?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#9: Aug 15 '07

re: alphabetizing parameters


Quote:

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.sort() methods; both use the same sorting method so
there's no need to worry about that.

kind regards,

Jos
Stang02GT's Avatar
Moderator
 
Join Date: Jun 2007
Location: USA
Posts: 1,152
#10: Aug 16 '07

re: alphabetizing parameters


If I use .......


Expand|Select|Wrap|Line Numbers
  1. Collections.sort(list, string.CASE_INSENSITIVE_ORDER);
do you think this will work?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#11: Aug 16 '07

re: alphabetizing parameters


Quote:

Originally Posted by Stang02GT

If I use .......


Expand|Select|Wrap|Line Numbers
  1. 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
Reply