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?
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.
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.
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.
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.
Please see the following code lines.
GB: Code removed.
Last edited by GregBrannon; June 13th, 2014 at 03:26 AM. Reason: Spoon-feeding
@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.
GregBrannon (June 13th, 2014)
@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.