-3

I need help with this problem, it seems it's not as easy I thought it would be. I've tried some options from early feedbacks but it didn't help. The thing is though that when userinput is like: i dont know the output should be displayed in screen: Sure, Sir Or when user-input is like: I dont know (I with upercase) the output should be: Sure, Sir. Or when user-input is like: idk (i dont know) the output should be: Sure, Sir. But neither of those things are happening so...Help!

import java.util.Scanner;
public class Something{
   static void Some(){
      Scanner input = new Scanner(System.in);
      String answer;
      
      System.out.println("Hello Sir, what can I do for you?");
      answer = input.nextLine();
      
      String [] idk = {"I dont know", "i dont know", "idk"};
      
      if(answer.equals(idk)) {
         System.out.println("Sure Sir ");
      } else {
         System.out.println("Sir, anything?");
      }
   }


   public static void main(String [] args) {
      Some();
   }
}

input: I dont know or i dont know or idk
output: Sir, anything?

I_code
  • 1
  • 1
  • 2
    Does this answer your question? [How do I determine whether an array contains a particular value in Java?](https://stackoverflow.com/questions/1128723/how-do-i-determine-whether-an-array-contains-a-particular-value-in-java) – PM 77-1 Mar 01 '21 at 19:44

2 Answers2

1
/*
   provide the input value and a listing of acceptable values
   returns true if acceptableValues contains value
*/

public static boolean anyMatch(String value, String... acceptableValues) {
    return Arrays.stream(acceptableValues).anyMatch((String t) -> value.equals(t));
}
0

It's better to use a collection (Set or List) to store the versions of the acceptable answers and then use method Collection::contains. Also it may be worth to store the strings written in lower case, and convert the input to lower case:

List<String> idk = Arrays.asList("i don't know", "i dont know", "idk");

if (idk.contains(answer.toLowerCase())) {
    System.out.println("Sure Sir ");
} else {
    System.out.println("Sir, anything?");
}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42