0

I write the code

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;
    }

and gets the approved for all Strings even if the above dosen't match with the user specified string

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
Chirag
  • 17
  • 4

1 Answers1

0

Compare Strings using equals. Also the implementation of getKeys() is too complicated and redundant.. Consider changing it to:

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";
    }
}

No need to use Arrays in the way you use it, nor a boolean switch..

You might want to check out this question to read about comparing Strings.

Community
  • 1
  • 1
Dropout
  • 13,653
  • 10
  • 56
  • 109
  • 1
    Sorry, but there is problem in the method in main program not in class. I checked it more time and found no problem in class code. I appreciate for the optimized code. – Chirag Oct 28 '14 at 14:01