How can I verify that an array of strings contain a certain string? [duplicate]

Internet of Things IoT Frameworks . 2 years ago

  0   1   0   0   0 tuteeHUB earn credit +10 pts

5 Star Rating 5 Rating
_x000D_ _x000D_ _x000D_ This question already has an answer here:_x000D_ _x000D_ _x000D_ How do I determine whether an array contains a particular value in Java?_x000D_ _x000D_ 28 answers_x000D_ _x000D_ _x000D_ _x000D_ _x000D_ Given : String[] directions = {"UP","DOWN","RIGHT","LEFT","up","down","right","left"}; String input = "Up"; How can I verify that an input from stdin is within the directions array or not ? I can make a loop and check each item with input using equal ,but I'm looking for a more elegant way. Regards,Ron

Posted on 16 Aug 2022, this text provides information on IoT Frameworks related to Internet of Things. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Write Your Comments or Explanations to Help Others



Tuteehub forum answer Answers (1)


profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ Convert the array of valid directions to a list: List valid = Arrays.asList(directions) Or just declare it directly as: List valid = Arrays.asList("UP", "DOWN", "RIGHT", "LEFT", "up", "down", "right", "left") You can then use the contains method: if (valid.contains(input)) { // is valid } else { // not valid } Note that this won't match a mixed case input such as "Up" so you might want to store just the uppercase values in the list and then use valid.contains(input.toUpperCase())
0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Internet of Things Links

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community