0

I have tried using the .indexof method but I can't figure out why it won't work, as all I need is to find if a value exists in an array ?


        final String subject = ((EditText)findViewById(R.id.subject)).getText().toString();


        final String  subjects[] ={"biology","business","chemistry","computer science","critical thinking",
                "economics","english language", "english literature","further mathematics","geography",
                "german","politics","history","ict","japanese","law", "mathematics","psychology",
                "religious studies","sociology","spanish"};

        final String  creative_subjects[] ={"art and design","fine art", "dance","design and technology",
                "design and textiles","fashion and textiles", "film studies ","music","performing arts",
                "photography", "physical education","graphics"};

        creative_subjects.indexOf(subject.toLowerCase());

john_smith
  • 31
  • 5
  • 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) – Muhammad Junaid Khalid Jan 30 '21 at 14:31

2 Answers2

2

Arrays don't have many useful methods. But you can wrap an array as a List:

Arrays.asList(creative_subjects).indexOf(...)
Arrays.asList(creative_subjects).contains(...)
// Etc.
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

Try this.

String[] values = {"AB","BC","CD","AE"};
boolean contains = Arrays.stream(values).anyMatch("s"::equals);

For further clarifications please visit This answer