Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

java - 我正在尝试检查电子邮件是否具有三个字母扩展名,例如“ .com”或“ .net”,并且此方法不起作用(I am trying to check if an email has a three letter extension such as “.com” or “.net” or not and this is not working)

this is not working and I cannot figure out why

(这是行不通的,我不知道为什么)

do {
    System.out.println("enter your work email");
    workEmail = scnr.nextLine();
    if (workEmail.substring(workEmail.length() - 4) != ".") {
        System.out.println("Please enter a valid email. example: [email protected]");
    }

} while (workEmail.substring(workEmail.length() - 4) != ".");
  ask by jackspurrrr translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your solution has two main issues:

(您的解决方案有两个主要问题:)

  1. You should never compare Strings using == or != .

    (您永远不要使用==!=比较字符串。)

    Use .equals() or even better StringUtils.equals() .

    (使用.equals()或更好的StringUtils.equals() 。)

  2. You're using subString() , when you want to use charAt() .

    (当您要使用charAt()时,您正在使用subString() charAt() 。)

    While chatAr would work, the solution from @YCF_L s comment using .endWith() is way superior.

    (虽然chatAr可以工作,但使用.endWith() @YCF_L注释中的解决方案更为出色。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...