0

When the code is 20, nonSmoking should be 1, but always returns null. After debugging I saw that it always returns null

How to set a variable nonSmoking correctly?

public static String mapSmokingAmenities(ContentFragmentList list){
        SolrContentFragment solrFragments = list.getFragment(SolrContentFragment.class);
        List<String> ausstattungPositions = solrFragments.getAusstattungPosition();
        String nonSmoking = new String();
        for(String code : ausstattungPositions){
            if(code == "20" || code == "36"){
                nonSmoking = "1";
            }
            if(code == "51"){
                nonSmoking ="0";
            }
        }
        return nonSmoking;
    }

enter image description here

anchy17
  • 85
  • 1
  • 1
  • 7
  • 5
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – maloomeister Aug 16 '21 at 06:34
  • It didn't help me – anchy17 Aug 16 '21 at 06:36
  • 1
    Have you verified that your DTO `solarFragments` is build correctly and got a list including data? Sidenote: if it is not related to a special business naming which is always german for you please convert all code to english. Only business objects should be names in your common language, rest in english – LenglBoy Aug 16 '21 at 06:38
  • The ContentFragmen is correct and returns a list of codes – anchy17 Aug 16 '21 at 06:39
  • my project requires these names – anchy17 Aug 16 '21 at 06:40
  • 1
    Me stupid. Strings are ofcause objects. using `==` you are comparing their address in memory. You need to use the `.equals()` method for a comparison of their value. So replace `==` by `"20".equals(code)`. Note? "20" will every tiome newly created. It would make sense to source it out as enum or constant. – LenglBoy Aug 16 '21 at 06:40
  • 2
    @AnidaMujezin "_It didn't help me_" ... did you check out the linked question (and its answers)? Because then you would find that comparing `String`s with `==` is not the way to go. – maloomeister Aug 16 '21 at 06:41
  • i changed == with equals and again my nonSmoking displays as null. see new picture above. – anchy17 Aug 16 '21 at 06:45
  • 3
    Your picture appears to show the debugger *just before* it assigns a value of "1". (It's also worth noting that null and a reference to an empty string are different things.) – Jon Skeet Aug 16 '21 at 06:45
  • 1
    `nonSmoking` is an empty String (as @JonSkeet says, just before it is assigned to). That method will never return null. – tgdavies Aug 16 '21 at 06:47
  • this is the image after the last changes, now it shows me an empty String (nonSmoking) – anchy17 Aug 16 '21 at 06:49
  • 1
    @LenglBoy: "Note? "20" will every tiome newly created. It would make sense to source it out as enum or constant." - no, that's not true. String literals already *are* constants. They don't involve creating a new instance every time execution goes through code using them. – Jon Skeet Aug 16 '21 at 06:49
  • 2
    Your code would *never* have returned a null reference. And again, you're showing the value as an empty string *before you assign a new value to it*. If you still believe there's a problem, please provide a [mcve], ideally as a console app, so that we can run it and reproduce the problem. That would be much, much more useful than debugger screenshots (especially when the code in the debugger no longer matches the text in the post.) – Jon Skeet Aug 16 '21 at 06:51
  • @user16320675 I changed it just to see the latest changes, i went back to the original code – anchy17 Aug 16 '21 at 07:22

2 Answers2

3

Comparing objects using == compares the address. You can find out if a object points to the same instance you want to work with. Only primitive datatypes (int/...) can be compared with value like that. For Strings as being object please use equals() method for the value compare.

If you have custom DTO/POJO where you want to compare value and not reference you need to overwrite the hashCode and equals method there.

LenglBoy
  • 297
  • 2
  • 8
2

Probably this could help

public static String mapSmokingAmenities(ContentFragmentList list){
    SolrContentFragment solrFragments = list.getFragment(SolrContentFragment.class);
    List<String> ausstattungPositions = solrFragments.getAusstattungPosition();
    String nonSmoking = "";
    for(String code : ausstattungPositions){
        switch(code) {
            case "20":
            case "36":
               nonSmoking = "1";
               break;
           case "51":
               nonSmoking = "0";
               break;
           default:
               // do nothing
               break;
        }
    }
    return nonSmoking;
}

You should use an enum for your codes and your result. Be aware, that you set nonSmoking every time being code 20, 36 or 51. Probably you should break your for loop. Don't create a new String but use an empty String "".

Michael Katt
  • 615
  • 1
  • 5
  • 18