2

I am trying to compare an array (of strings) to a regular string from an input. Is this possible? I have my test code and it's mostly just says

incomparable types java.lang.string and java.lang.string[]

This is my test code:

 String[] yes = new String[5];
yes[0] = "yes";
yes[1] = "yeah";
yes[2] = "sure";
yes[3] = "yupp";
yes[4] = "okay";
System.out.println("Input");
String input = scan.next();
if ((input).equalsIgnoreCase(yes)) {
  System.out.println("compared!");
}

4 Answers 4

2

The most efficient solution will be to create a set of Strings and then call set.contains to see if the input string is present in the set. Sample code:

 HashSet<String> stringSet = new HashSet<String>();
    //All string are already lower case so no need of toLowerCase() here
    stringSet.add("yes");
    stringSet.add("yeah");
    stringSet.add("sure");
    stringSet.add("yupp");
    stringSet.add("okay");
    System.out.println("Input");
    String input = scan.next();
    if (stringSet.contains(input.toLowerCase())) {
      System.out.println("compared!");
    }
Sign up to request clarification or add additional context in comments.

5 Comments

You need to convert the input to lower case for this to work. Also it requires the strings in the set to be lowercase (which is the case here, but should be mentioned...).
Suggestions: Set<String> stringSet = new HashSet<String>(); (code to interface) then: Collections.addAll(stringSet, "yes", "yeah", "sure", "yupp", "okay");.
@fabian String comparisons are case sensitive. I have added a test code for you. You can check this ideone.com/iJRNTN
@AbhishekGarg: I know String comparison is case sensitive, but since the OP wants to use equalsIgnoreCase you need a workaround to use Set. In this case simply converting every sting to lower case would work, since s1.equalsIgnoreCase(s2) iff s1.toLowerCase().equals(s2.toLowerCase()) (assumning no null objects involved)
oh ok my bad I missed the equalsIgnoreCase part. Made the edit thanks
1

You need to specify which index in the array to check it against.

All five entries in the array are different, and Java doesn't know which one to check.

You may want to use a for loop to iterate through all the possibilities, and compare each entry to the input.

2 Comments

is there a simple way to check all of them together or would it be something along the lines of if((input).equalsIgnoreCase(yes[0,1,2,3,4])) {
It's a three-liner. for (String possibility : yes) if (input.equalsIgnoreCase(possibility)) System.out.println("Compared!");
1

I would use some form of iteration to step through the array one element at a time:

for (String compare : yes){
    if ((input).equalsIgnoreCase(compare)) {
    System.out.println("compared!");
}}

Or something along these lines after you read in the input.

Comments

0

Assuming values in yes are all lower-case

String[] yes = new String[]{"yes", "yeah", "sure", "yupp", "okay"};
System.out.println("Input");
String input = scan.next();
if (Arrays.asList(yes).contains(input.toLowerCase())) {
  System.out.println("compared!");
}

or with lambdas, ignoring case, as suggested by fabian in comments

String[] yes = new String[]{"yes", "yeah", "sure", "yupp", "okay"};
System.out.println("Input");
String input = scan.next();
if (Arrays.stream(yes).anyMatch(input::equalsIgnoreCase)) {
  System.out.println("compared!");
}

1 Comment

Arrays.stream(yes).anyMatch(input::equalsIgnoreCase) wouldn't require the yes array to be all lowercase.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.