Results 1 to 11 of 11

Thread: a small advice

  1. #1
    java_newby is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default a small advice

    hello, so i need small advice on my problem.

    i have a linked list of words, such as "and". i need to get each word and sort it alphabeticly. i.e. i believe "and" would become "adn". i am able to iterate through my linked list. i have the working code. but i have no idea how to sort the characters in a string, as i must not use any java API classes other than string. this is the only class where i can call methods.

    can someone give me advice in the right direction, simply as i have not studied java for long.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Create a char array given a String (read the API documentation); sort the array and create a String again, given the sorted array.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    java_newby is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    but i cannot sort the array because i cannot use array.sort

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by java_newby View Post
    but i cannot sort the array because i cannot use array.sort
    Can't you write a little sort method yourself?

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    java_newby is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    i would not have idea how to do this. i do not want you to write code for me but could you give tips of how to do the sorting as this is the part where i have hit a brick wall. thank you

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by java_newby View Post
    i would not have idea how to do this. i do not want you to write code for me but could you give tips of how to do the sorting as this is the part where i have hit a brick wall. thank you
    The easiest to write, but it's very inefficient, method is the bubble sort method, It is based on a simply notion: if an element to the left of another element is larger than the other element, the array isn't sorted yet. A base for the algorithm would be:

    Java Code:
    while not done:
       assume done
       for all elements array[i] and array[i+1]:
          check if the elements are in order
          if not, swap them and set done to false again
    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  7. #7
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    you can use TreeSet for this purpose.
    u just add elements to it and u can retrieve from it in sorted order.....

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by jomypgeorge View Post
    you can use TreeSet for this purpose.
    u just add elements to it and u can retrieve from it in sorted order.....
    That doesn't work; the OP has to sort the words themselves, e.g.

    - and
    - banana
    - java

    becomes:

    - adn
    - aaabnn
    - aajv

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  9. #9
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    sorry i got it wrong.....

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by jomypgeorge View Post
    sorry i got it wrong.....
    No need to apologize, as a matter of fact I suffered a bit from tunnel vision: you can sort individual words using a SortedMap<Character, Int> but it's a bit of a detour; it's much cheaper to write your own sorting method.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  11. #11
    java_newby is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    i see. this works well. i need advice on last problem for my project. i will post as new thread as this problem is now solved, thank you very very much

Similar Threads

  1. Advice needed :)
    By kagster in forum New To Java
    Replies: 1
    Last Post: 11-16-2010, 02:04 AM
  2. Advice on writing a bot to...?
    By Krooger in forum Advanced Java
    Replies: 1
    Last Post: 07-22-2010, 01:20 PM
  3. Im new n looking for an advice
    By azlynn in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 03:47 AM
  4. Need advice on JSP with bean
    By butterhero in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-18-2009, 11:40 AM
  5. Some advice please!
    By awebbtt in forum New To Java
    Replies: 3
    Last Post: 02-02-2009, 08:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •