示例生成代码:910love009tre
我想检查生成的代码中是否有特定的单词。我正在尝试使用contains-method,但它似乎没有给我所需的输出。
import java.security.SecureRandom;
import java.util.HashSet;
import java.util.Set;
public class AlphaNumericExample {
public static void main(String[] args) {
enter code here
AlphaNumericExample example = new AlphaNumericExample();
Set<String> codes = new HashSet<>();
for (int x = 0;x< 100 ;x++ ) {
codes.add(example.getAlphaNumeric(16));
System.out.println(codes.contains("love"));//Im trying to check if the generated codes that have been stored in hashset have form a word
}
System.out.println("Size of the set: "+codes.size());
}
public String getAlphaNumeric(int len) {
char[] ch = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
char[] c = new char[len];
SecureRandom random = new SecureRandom();//
for (int i = 0; i < len; i++) {
c[i] = ch[random.nextInt(ch.length)];
}
return new String(c);
}
}
哈士奇WWW
慕标5832272
守候你守候我
相关分类