String Not equal Array element.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Win Win Phyu
    New Member
    • Aug 2019
    • 1

    String Not equal Array element.

    Hi!!

    How do I write the if condition to say user string input does not equal the values inside the array. Your response will be appreciated.
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 655

    #2
    Code:
    if(!str1.equals(str2)){
      ...
    }
    equals() method compares two strings and if the content of both strings is the same, it returns true. Not operator is used according to the need.

    Comment

    • Ishan Shah
      New Member
      • Jan 2020
      • 47

      #3
      You can use contains method to achieve your requirement :

      Code:
      if(!Arrays.asList(arr).contains(user_string)){
      	...
      }
      In Following code, contains method returns false if it contains user_string in array else it will return true

      Comment

      • dev7060
        Recognized Expert Contributor
        • Mar 2017
        • 655

        #4
        My reply was incomplete. The code should be inside a loop to traverse through all the values.

        Comment

        Working...