简体   繁体   中英

If condition Error on compiling - Specific question

I am trying to detect if the variables f1 and f2 have the letter v or b and then check if both words have the same length. I do not know why but when I run this code says that there are three errors on my if condition

    Scanner ask = new Scanner(System.in);
    String f1 = (ask.nextLine()).toLowerCase();
    String f2 = (ask.nextLine()).toLowerCase();
    boolean yes  = false;
    
    if((f1.indexOf("v") > -1 || f1.indexOf("b")) and (f2.indexOf("v") > -1 || f2.indexOf("b")) and (f1.length() == f2.length() )){
        yes = true;
    }

Error at compile

Solution.java:12: error: ')' expected
        if((wrd.indexOf("v") > -1 || wrd.indexOf("b")) and (wrd2.indexOf("v") > -1 || wrd2.indexOf("b")) and (wrd.length() == wrd2.length() )){
                                                      ^
Solution.java:12: error: ';' expected
        if((wrd.indexOf("v") > -1 || wrd.indexOf("b")) and (wrd2.indexOf("v") > -1 || wrd2.indexOf("b")) and (wrd.length() == wrd2.length() )){
                                                                                                        ^
Solution.java:12: error: ';' expected
        if((wrd.indexOf("v") > -1 || wrd.indexOf("b")) and (wrd2.indexOf("v") > -1 || wrd2.indexOf("b")) and (wrd.length() == wrd2.length() )){
^
3 errors
Exit Status

1

The logical "and" operator in Java is && , not the word and :

if ((f1.indexOf("v") > -1 || f1.indexOf("b")) && 
    (f2.indexOf("v") > -1 || f2.indexOf("b")) &&
    (f1.length() == f2.length())) {

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM