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 6 of 6

Thread: Displaying number of occurances of each word in the array

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Displaying number of occurances of each word in the array

    The print statement is getting caught in the loop, but I'm not sure where to place the print statement. Am I close to the right answer in the last method, or should I erase it and start over with a different approach?
    Last edited by The Deacon; July 7th, 2014 at 09:20 PM.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Displaying number of occurances of each word in the array

    If you print within the loop you will not get your desired output. That is because you can not forsee the future and you never know how many flowers of the same name might still come at a later position in the array.

    What you could do is have 2 for-loops. In the first loop you count the occurances, in the second loop you print them.
    The first loop would iterate over your flower array, count the flowers and populate a data structure used to save the results.
    The second loop would iterate over this other data structure and print the contents.
    The other data structure could be a Map<String, Integer> or something else entirely.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Displaying number of occurances of each word in the array

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

    I'm going to suggest you LOOK at the existing output, compare it to the desired output, and then review your code and correct it to achieve the desired output.

    You are printing the desired output and THEN SOME. Correct your code so that it stops printing the extra lines:

    Existing output:
    Daisy - 4
    Daisy - 3
    Daisy - 2
    Daisy - 1
    Rose - 2
    Rose - 1
    Sunflower - 1

    Desired output:
    Daisy - 4
    Rose - 2
    Sunflower - 1

    Examine your code and determine how to remove the extra lines.

  4. #4
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Displaying number of occurances of each word in the array

    Please see the following code lines.

    GB: Code removed.
    Last edited by GregBrannon; June 13th, 2014 at 03:26 AM. Reason: Spoon-feeding

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Displaying number of occurances of each word in the array

    @chsvravikumar@gmail.com
    Please do not post a solution on this forum. Nobody is helped if you give away answers.
    People can only learn if they do something by themself.

  6. The Following User Says Thank You to Cornix For This Useful Post:

    GregBrannon (June 13th, 2014)

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Displaying number of occurances of each word in the array

    @chsvravikumar@gmail.com: Please review The Problem with Spoon-feeding to understand this forum's policy about providing answers.

    Further, welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members. Post your code correctly.

Similar Threads

  1. Scanning and displaying every word from a website source code Java
    By Lilac in forum What's Wrong With My Code?
    Replies: 12
    Last Post: February 19th, 2014, 02:10 PM
  2. pls help me with word occurances program
    By kashif in forum Collections and Generics
    Replies: 6
    Last Post: August 16th, 2011, 03:16 PM
  3. pls help me with word occurances program
    By kashif in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 16th, 2011, 03:16 PM
  4. pls help me with word occurances program
    By kashif in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 16th, 2011, 09:39 AM
  5. [SOLVED] Reading integers from a file, but it's only displaying the first number repeatedly..?
    By jessica202 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 29th, 2011, 08:16 AM