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

Internet of Things IoT Frameworks 3 years ago

8K 1 0 0 0

_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

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 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.

Similar Forum


Q

Azure IoT hub and sending messages with mosquitto_pub

_x000D_ _x000D_ I'm trying to send some simple message with mosquitto_pub to Azure IoT HUB but faced...
Q

How to generate AWS bootstrap certificates with java SDK?

_x000D_ _x000D_ According to this docs I need to generate so-called bootstrap certificates for my Io...
Q

is it possible to do 3way handshake only one time with mqtt communication?

_x000D_ _x000D_ I am using mosquitto_pub to publish the data with TLS using a topic. I am using mosq...

Important Internet of Things Links