Java 如何检查字符串是否等于先前定义的字符串数组中的任何元素
我创建了一个Java 如何检查字符串是否等于先前定义的字符串数组中的任何元素,java,arrays,string,arraylist,Java,Arrays,String,Arraylist,我创建了一个ArrayList,其中包含从internet检索到的常用密码,并初始化了一个名为commonPasswords的数组。我想检查用户输入的密码是否与数组中的任何密码匹配。然而,这似乎不起作用。我不知道为什么。任何帮助都将不胜感激 我刚开始学习如何编程,所以我在这个领域是个新手。谢谢 int commonpass = 0; int check = 0; while (commonpass == 0) { if (password.equals(commonPasswords.g
ArrayList
,其中包含从internet检索到的常用密码,并初始化了一个名为commonPasswords
的数组。我想检查用户输入的密码是否与数组中的任何密码匹配。然而,这似乎不起作用。我不知道为什么。任何帮助都将不胜感激
我刚开始学习如何编程,所以我在这个领域是个新手。谢谢
int commonpass = 0;
int check = 0;
while (commonpass == 0) {
if (password.equals(commonPasswords.get(check))) {
score = 0;
}
check++;
if (check >= commonPasswords.size()) {
commonpass++;
}
}
改用,比如
if(commonPasswords.contains(password)){
System.out.println("Password is not safe");
}
使用Java8,您可以按如下方式进行操作
List<String> commonPasswords = Arrays.asList("A", "B", "C");
return commonPasswords.stream().anyMatch(str -> str.equals(password));
List commonPasswords=Arrays.asList(“A”、“B”、“C”);
返回commonPasswords.stream().anyMatch(str->str.equals(password));
正如钱德勒所说,你应该使用commonPasswords.contains(str)
而不是password.equals(commonPasswords.get(check))
或
因为您是初学者,我为您编写了以下代码,演示了4种可能的非常简单的方法来实现您的愿望。 初学者不建议使用Java8流API和Lambda表达式
List<String> commonPasswords = Arrays.asList("touraj", "ttt", "toraj", "123");
String userPassword = "123";
//First Way:
if (commonPasswords. contains(userPassword)) {
System.out.println("Password Found");
} else
{
System.out.println("Password Not Found");
}
//Second Way: foreach :: not suggested for beginners
for (String commonPassword : commonPasswords) {
if (commonPassword.equals(userPassword)) {
System.out.println("Password Found");
// here i use break after finding password to exit loop in order to not wasting cpu
break;
}
}
//Third Way: simple for loop :: suggested for beginners
for (int i = 0; i <commonPasswords.size() ; i++) {
if (commonPasswords.get(i).equals(userPassword)) {
System.out.println("Password Found");
}
}
//Forth way: Using Java 8 Stream Api :: Not Suggested for beginners like you
boolean isPassFound = commonPasswords.stream().anyMatch(pass -> pass.equals(userPassword));
if (isPassFound) {
System.out.println("Password Found.");
}
List commonPasswords=Arrays.asList(“touraj”、“ttt”、“toraj”、“123”);
字符串userPassword=“123”;
//第一种方式:
if(commonPasswords.contains(userPassword)){
System.out.println(“找到密码”);
}否则
{
System.out.println(“未找到密码”);
}
//第二种方法:foreach::不建议初学者使用
用于(字符串commonPassword:commonPasswords){
if(commonPassword.equals(userPassword)){
System.out.println(“找到密码”);
//在这里,为了不浪费cpu,我在找到密码后使用break退出循环
打破
}
}
//第三种方法:简单for循环::建议初学者使用
对于(int i=0;i pass.equals(userPassword));
如果(isPassFound){
System.out.println(“找到密码”);
}
注意:为了理解我在这里建议的java 8代码,您首先需要学习面向对象和接口,然后学习匿名方法,然后学习lambda表达式,然后学习Stream API……但我希望java 8版本是自解释的,并且在某种程度上类似于人类语言 OP说他想
检查用户输入的密码是否匹配
,因此我认为他们的意思与相同
如果OP确实在寻找相同的条目,则OP应该使用String.equals
,而不是Array.contains
。因为Array.contains
可能会给他一个假阳性结果
private void checkPasswordExists(){
List<String> passwordList = Arrays.asList("pass1", "pass2", "pass12", "pass123");
int countContains = 0;
String userPassword = "pass1";
for(String password : passwordList){
if(password.contains(userPassword)){
countContains++;
}
}
int countEquals = 0;
for(String password : passwordList){
if(password.equals(userPassword)){
countEquals++;
}
}
System.out.println("Password countContains = " + countContains);
System.out.println("Password countEquals = " + countEquals);
}
使用
commonPasswords.contains(password)
而不是可能重复的contains
已经有一个循环。无需再次添加。只是增加了复杂性哦,是的,我不知道我在想什么。修正了。你可以只做返回公共密码。包含(密码)
是的,你可以,但是他是编程新手,我认为if语句更直观。不,它不直观。这是坏习惯。对于初学者来说,比较字符串与=
似乎很直观。但那是错误的,你把事情搞得太复杂了。不要因为必须使用lambda而只使用lambda,在有意义的时候使用它。没有什么比一个简单的包含的更好了,在这里你也可以做。anyMatch(password::equals)代码>
List<String> commonPasswords = Arrays.asList("touraj", "ttt", "toraj", "123");
String userPassword = "123";
//First Way:
if (commonPasswords. contains(userPassword)) {
System.out.println("Password Found");
} else
{
System.out.println("Password Not Found");
}
//Second Way: foreach :: not suggested for beginners
for (String commonPassword : commonPasswords) {
if (commonPassword.equals(userPassword)) {
System.out.println("Password Found");
// here i use break after finding password to exit loop in order to not wasting cpu
break;
}
}
//Third Way: simple for loop :: suggested for beginners
for (int i = 0; i <commonPasswords.size() ; i++) {
if (commonPasswords.get(i).equals(userPassword)) {
System.out.println("Password Found");
}
}
//Forth way: Using Java 8 Stream Api :: Not Suggested for beginners like you
boolean isPassFound = commonPasswords.stream().anyMatch(pass -> pass.equals(userPassword));
if (isPassFound) {
System.out.println("Password Found.");
}
private void checkPasswordExists(){
List<String> passwordList = Arrays.asList("pass1", "pass2", "pass12", "pass123");
int countContains = 0;
String userPassword = "pass1";
for(String password : passwordList){
if(password.contains(userPassword)){
countContains++;
}
}
int countEquals = 0;
for(String password : passwordList){
if(password.equals(userPassword)){
countEquals++;
}
}
System.out.println("Password countContains = " + countContains);
System.out.println("Password countEquals = " + countEquals);
}
Password countContains = 3
Password countEquals = 1