利用String類或StringBuffer類的方法,對輸入的Email位址進行有效性驗證。提示: 1)Email位址中應包含“@”和“.”符号; 2)“@”符号應該在“.”符号之前; 3
import java.util.Scanner;
public class Emailshuzu {
public static void main(String[] args) {
String Email = null;
Scanner scanner = new Scanner(System.in);
System.out.print("請輸入Email位址 :");
Email = scanner.nextLine();
if (Email.indexOf('@') != -1 && Email.indexOf('.') > Email.indexOf('@')) {
System.out.println("郵箱位址有效");
} else {
System.out.println("郵箱位址無效");
}
}
}