Skip to content
Advertisement

Find all matching elements in Array – Java

At the moment my code is only giving me the first matching result. The user inputs their desired room price and at the moment it will only display the first match. In the case the user inputs ’60’ to the console it should display 3 results. I imagine i’ll need another forloop and if statement after it prints to the console but not sure how to execute

JavaScript

Any other information needed feel free to ask

Advertisement

Answer

If you only want to print the information move the print commands inside the loop and remove the break i.e.

JavaScript

You could also save all the objects in a list with the first loop and then in a second loop iterate over the list and print the information i.e.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement