微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 Java 中的 if 语句检查字符串是空还是空?

如何解决如何使用 Java 中的 if 语句检查字符串是空还是空?

我需要首先使用 RegodocumentType.getByValue(createOrderRequest.getIdDocument().getIdType())

获取枚举本身

然后检查空值,如果不为空,则返回枚举值。否则,它将返回 createOrderRequest.getIdDocument().getIdType() 作为认值。

那么,我如何重构代码以仅使用一个 if 语句来满足 NRIC/11B 和 FIN smag 值以使用 RegodocumentType 枚举的 rego 值?

这是我的枚举:

public enum RegodocumentType {
    
    NRIC_11B("NRIC/11B",IdentityType.NRIC.tovalue()),FIN("Employment Pass",IdentityType.EM_PASS.tovalue()),;
    private static final Map<String,RegodocumentType> BY_SMAG_VALUE = new HashMap<>();
    static {
        for (RegodocumentType identityType : values()) {
            BY_SMAG_VALUE.put(identityType.getSmagValue().toLowerCase(),identityType);
        }
    }
    private final String smagValue;
    private final String regovalue;
    RegodocumentType(String smagValue,String regovalue) {
        this.smagValue = smagValue;
        this.regovalue = regovalue;
    }
    public String getSmagValue() {
        return smagValue;
    }
    public String getRegovalue() {
        return regovalue;
    }
    public static RegodocumentType getBySmagValue(String smagValue)
    { return BY_SMAG_VALUE.get(smagValue.toLowerCase()); }
}

解决方法

    String something = "";
    if(something == null || something.isEmpty()) {
        // return A
    } else {
        // return B
    }

    String something = "";
    return something == null || something.isEmpty() ? [value A] : [value B];
,

createOrderRequest.getIdDocument().getIdType() 在你的情况下似乎是一个字符串。您不能从 getBySmagValue 返回字符串。考虑返回 null 或抛出异常:

public static RegoDocumentType getBySmagValue(String smagValue) { 
    if (smagValue == null) reutrn null;
    return BY_SMAG_VALUE.get(smagValue.toLowerCase()); 
}

public static RegoDocumentType getBySmagValue(String smagValue) { 
    if (smagValue == null) throw new IllegalStateException();
    return BY_SMAG_VALUE.get(smagValue.toLowerCase()); 
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。