I made a program that check a string if there is at least one vowel in the string. But when I input a string that have a vowel in it, the result is ""your password word is not acceptable" instead of "your password word is acceptable".
can someone show me where I do wrong? thank you!
here is the program:
import java.util.Scanner;
public class checker {
static Scanner input= new Scanner(System.in);
public static void main(String[] args) {
String password;
System.out.println("enter your password:");
password= input.next();
String vowel[]= {"a","e","i","o","u"};
for(int i=0; i<5;i++) {
boolean check[] = new boolean[5];
check[i]=password.contains(vowel[i]);
if(i==vowel.length-1&&check[0] ==false && check[1]==false && check[2]==false && check[3]==false && check[4]==false) {
System.out.println("your password word is not acceptable");
}else System.out.println("your password is acceptable");
}
}
}
You can do this in the following way:
import java.util.Scanner;
public class checker {
static Scanner input= new Scanner(System.in);
public static void main(String[] args) {
String password;
System.out.println("enter your password:");
password= input.next();
String vowel[]= {"a","e","i","o","u"};
boolean check = false;
for(int i=0; i<5;i++) {
check = password.toLowercase().contains(vowel[i]);
if(check){
break;
}
}
if(!check){
System.out.println("your password word is not acceptable");
}else {
System.out.println("your password is acceptable");
}
}
}
You can easily check if a string contains vowel or not using regular expression.Please check the below code:
public static void main(String[] args) {
String input = "tssta";
String regex= ".*[AEIOUaeiou].*";
if(input.matches(regex)){
System.out.println("your password word is acceptable");
}else {
System.out.println("your password word is not acceptable");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.