-1
public void setSpeciality(String spec) {
    if(specialities.stream().anyMatch(s-> s.equalsIgnoreCase(spec))) {
        speciality =spec;
    }
    else
    speciality = "General Practitioner";
}

I need to search an array list called specialities (case insensitive) ^ see my attempt at an answer^ but cant figure out what in my answer is wrong.

shmosel
  • 49,289
  • 6
  • 73
  • 138
HughM
  • 1
  • 6
  • Easiest way, if you don't care about the case of the data returned, is to just store lowercase strings in your list, and then compare the input, lowercased, using `List#contains`. See the duplicate link for a Java 8 solution as well. – Tim Biegeleisen Oct 19 '18 at 06:06
  • 1
    You need to add an else block and move `speciality = "General Practitioner";` into it – Peter Chaula Oct 19 '18 at 06:08
  • Also, what result are you expecting? What result are you getting? – Peter Chaula Oct 19 '18 at 06:09
  • @TimBiegeleisen I don't the dupe target is correct. The OP understands how to check if an element is in the list case insensitively. OP's problem is _getting_ the element that matches the predicate. – Sweeper Oct 19 '18 at 06:09
  • Your method will always set `speciality` to "General Practitioner" no matter if the `if` statement is true or not. Looks like you are missing the `else` part of your `if` statment. – Russell Jonakin Oct 19 '18 at 06:10
  • @JustSomeDude else statement didnt fix it – HughM Oct 19 '18 at 06:11
  • @HughM how is your answer wrong? can you explain what the output is that you are calling wrong? – Russell Jonakin Oct 19 '18 at 06:23
  • @HughM check your input parameter that you are passing in setSpeciality method – Pandey Amit Oct 19 '18 at 06:31

3 Answers3

4

anyMatch only returns true or false indicating whether there is at least 1 element in the stream that satisfies the predicate.

You should use filter and findFirst, which returns the first element that matches the predicate:

speciality = specialities.stream()
                 .filter(spec::equalsIgnoreCase)
                 .findFirst()
                 .orElse("General Practitioner");
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Its giving me the error "cannot convert from Stream to String" – HughM Oct 19 '18 at 06:08
  • @TheScientificMethod According to the comments in OP's code, `speciality` should have the value of an element in the list, not `spec`. – Sweeper Oct 19 '18 at 06:10
  • 1
    @TheScientificMethod Let's say `spec` is "ONCOLOGIST" and the list contains "Oncologist", `speciality` should be `"Oncologist"` after the method is called. – Sweeper Oct 19 '18 at 06:12
  • @Sweeper i got your point, but he can do it like `speciality =spec.toLowerCase();` – The Scientific Method Oct 19 '18 at 06:15
  • 2
    @TheScientificMethod How can you be sure that the string in the list is all lowercase? – Sweeper Oct 19 '18 at 06:17
  • @HughM You are not allowed to edit what? `specialities` is a `List` right? Then it should work. You should replace your whole method body with the code snippet. – Sweeper Oct 19 '18 at 06:21
  • @Sweeper I figured it out based on your response! Thanks – HughM Oct 19 '18 at 06:24
  • 1
    Since `String.equalsIgnoreCase` is symmetric we could also write the filter using a method reference: `.filter(spec::equalsIgnoreCase)` – LuCio Oct 19 '18 at 06:27
0

just include, speciality =spec.toLowerCase(); and convert the first character to uppercase

public void setSpeciality(String spec) {
    if(specialities.stream().anyMatch(s-> s.equalsIgnoreCase(spec))) {
        speciality =spec.toLowerCase();
        speciality = String.valueOf((speciality.charAt(0)).toUpperCase())  + speciality.substring(1) ;
    }
    else {
          speciality = "General Practitioner";
    }
}
The Scientific Method
  • 2,374
  • 2
  • 14
  • 25
-1

You are overriding speciality so every time you will be getting same result.

change code like below

    public void setSpeciality(String spec) {
       if(specialities.stream().anyMatch(s-> s.equalsIgnoreCase(spec))) {
            speciality =spec;
       }else{
           speciality = "General Practitioner";
       }
     }
Pandey Amit
  • 657
  • 6
  • 19