Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Sorting by numbers of words in each sentence that is in ArrayList index.

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    My Mood
    Angelic
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Sorting by numbers of words in each sentence that is in ArrayList index.

    Hello! I am a beginner!I have a task to sort sentences by its word quantity, that is in Arraylist.For example,here is the code of my Array example:

    List<String> numberOfWords=new ArrayList<>();

    numberOfWords.add("Happy coding!"); //2 words or 3 with(!),I don't know
    numberOfWords.add("He plays football in the playground everyday"); //7
    numberOfWords.add("She is cooking an omellette"); //5
    numberOfWords.add("I am coding in Java"); //5
    numberOfWords.add("I want to code very well"); //6

    // And the result must be like this: So, please could anyone give me some advice on how to come to this outcome??? Thanks!!!

    Happy coding! //2 words
    I am coding in Java //5 words
    She is cooking an omellette //5words
    I want to code very well //6 words
    He plays football in the playground everyday //7words


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Saint-Petersburg, Russia
    Posts
    33
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Sorting by numbers of words in each sentence that is in ArrayList index.

    You need to write Comparator for your sentences, which compares strings by the number of words (or spaces) in them - to measure number of words you can use String.replaceAll("\\S", "!").replaceAll("\\s", "") to replace all words with exclamation marks and remove spaces - so that length of the result equals to number of words.

    Then you can simply call Collections.sort() and pass your list and your comparator here.

  3. The Following User Says Thank You to rodiongork For This Useful Post:

    Aika (October 26th, 2013)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,281
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Sorting by numbers of words in each sentence that is in ArrayList index.

    Look at using the Collections class's sort() method. You will need to write a Comparator for it to use.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Aika (October 26th, 2013)

  6. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,289
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: Sorting by numbers of words in each sentence that is in ArrayList index.

    You can use code formatting and/or highlight tags to make your posted code look "pretty" rather than doing the formatting yourself. You can learn more about that in the Announcement topic at the topic of the sub-forum with other useful info for new comers.

    Did you notice that the number of words is the number of spaces + 1? Maybe you could use that with a String method somehow. Review the String API and the existing methods to see if you get any ideas.

  7. The Following User Says Thank You to GregBrannon For This Useful Post:

    Aika (October 26th, 2013)

  8. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,281
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Sorting by numbers of words in each sentence that is in ArrayList index.

    If you don't understand my answer, don't ignore it, ask a question.

  9. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    5
    My Mood
    Angelic
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Sorting by numbers of words in each sentence that is in ArrayList index.

    Based on all your recomendations, I wrote this code but I can't find what's wrong with that?Please, look and point at my mistakes! There are no fatal errors, just the result isn't correct, not that I wanted !The order of the sentences isn't correct!
    int i = 0;
            String words=null;
     
            for (i = 0; i < numberOfWords.size(); i++) {
                words = numberOfWords.get(i).replaceAll("[a-zA-z]", "!").replaceAll("\\s*", "");
                Collections.sort(numberOfWords, new SortingSentence());
                System.out.println(numberOfWords.get(i));
            }
        }
     
        @Override
        public int compare(String t, String t1) {
            if (t.length() > t1.length()) {
                return 1;
            } else if (t.length() < t1.length()) {
                return -1;
            } else {
                return 0;
            }
        }
    }

  10. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,281
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Sorting by numbers of words in each sentence that is in ArrayList index.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Please explain what happens when you compile and execute the program.
    If there are errors copy the full text and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: August 26th, 2013, 05:41 PM
  2. Replies: 12
    Last Post: March 17th, 2013, 02:38 AM
  3. Arranging words in a sentence in alphabatical order.
    By J2000 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 24th, 2011, 10:04 AM
  4. Replies: 4
    Last Post: November 14th, 2010, 05:02 PM
  5. Help with sorting numbers
    By planktonx in forum Collections and Generics
    Replies: 2
    Last Post: October 5th, 2010, 09:01 AM