Results 1 to 5 of 5
  1. #1
    EdwinTeo is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Searching for text and putting them at the top

    Need help with some searching and sorting.. My program is reading from a text file and contains many sentences.. I have to search for a word from the sentences and then sort them at the top of the text file..

    s is read from a text file
    some examples of sentences in the text file
    1) "blahblahblahblah. blahblahblahblah that is blahblahblahblah"
    2) "wooowoooowooo wooowooowoo"
    3) "that uiuiuiuiuiiu"

    here is part of my codes which i couldnt continue, i have to put the 1) and 3) at the top of the text file
    ArrayList<String> list = new ArrayList<String>(Arrays.asList(s));
    if(list.contains("that"))
    {
    //how do i get that array(s) with "that" and move it to the top?
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: Searching for text and putting them at the top

    You don't want to see if the list contains "that"; you want to see if a line in the list contains "that". That will involve iterating over each and every line.

    Do you really need to sort, or do you just need to move the lines with the search word to the top of the list?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    EdwinTeo is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: Searching for text and putting them at the top

    I need to move all lines with "that" to the top of the list. In a way sorting the list by beginning with lines that contains "that". Order of subsequent lines that does not include "that" does not matter.

    Thanks

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: Searching for text and putting them at the top

    I'd apply a lazy trick myself; build a second list.

    1. iterate over source list
    2. if line contains "that", add to front of list 2
    3. otherwise add to back of list 2

    pretty simple, if you research how to use lists.
    Last edited by gimbal2; 03-12-2014 at 11:29 AM. Reason: Engrish
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    EdwinTeo is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: Searching for text and putting them at the top

    alright, will try that.. thanks again

Similar Threads

  1. Searching text in file
    By RafalD in forum New To Java
    Replies: 8
    Last Post: 12-12-2012, 10:23 AM
  2. Need Help putting 3 text strings on a jpeg.
    By Jadeclan in forum New To Java
    Replies: 5
    Last Post: 10-25-2011, 02:22 AM
  3. Replies: 2
    Last Post: 10-21-2010, 02:30 PM
  4. Replies: 3
    Last Post: 06-02-2010, 06:58 PM
  5. searching a row using text fields
    By bigj in forum New To Java
    Replies: 1
    Last Post: 02-03-2010, 11:28 AM

Posting Permissions

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