如何从String缓冲区逐行读取数据并与另一个String匹配

时间:2014-10-28 13:28:20

标签: java

我写了代码

public class keys {

    private static final String[] keyss = {
        "HGDLV-KLLQC-DM3ON-MAUOX-FNQK5",
        "HQKLD-NYRDL-NJXCV-BVCKF-NRKLC", 
        "LDFCV-BDJCQ-VMFQV-FNRKC-NFCFL"
         //and many more
    };

    public static String getKey(String key) {
        boolean b = Arrays.asList(keyss).contains(key);
        if (b == true) {
            key = "approved";
        } else {
            key = "unapproved";
        }
        return key;
    }

并获得所有字符串的批准,即使上述内容与用户指定的字符串

不匹配

1 个答案:

答案 0 :(得分:0)

使用equals比较字符串。此外,getKeys()的实现过于复杂和冗余。请考虑将其更改为:

public class Keys {
    private static final String[] keys = {
        "HGDLV-KLLQC-DM3ON-MAUOX-FNQK5",
        "HQKLD-NYRDL-NJXCV-BVCKF-NRKLC", 
        "LDFCV-BDJCQ-VMFQV-FNRKC-NFCFL"
         //and many more
    };

    public static String getKey(String key) {
        for(String current : keys){
            if(current.equals(key)){
                return "approved";
            }
        }
        return "unapproved";
    }
}

您无需使用Arrays,也不需要boolean切换..

您可能需要查看this question以了解有关比较字符串的信息。