Java check file name and email address
•
Java
public static void main(String[] args) { boolean filecon = false; // 判断文件名是否合法 boolean emailcon = false; // 判断邮箱地址是否合法 System.out.println("************ 欢迎进入作业提交系统 ************"); Scanner input = new Scanner(system.in); System.out.println("请输入要提交的Java文件名称:"); String name = input.next(); // 获取输入的Java文件名 System.out.println("请输入要提交到的邮箱地址:"); String email = input.next(); // 获取输入的邮箱地址 // 检查输入的文件名是否合法 int index = name.lastIndexOf('.'); // 获取"n"所在的位置 // 判断合法 if (index != -1 && name.charAt(index + 1) == 'j' && name.charAt(index + 2) == 'a' && name.charAt(index + 3) == 'v' && name.charAt(index + 4) == 'a') { filecon = true; } else { System.out.println("输入的文件名无效!"); } // 检查邮箱地址是否合法 if (email.indexOf('@') != 1 && email.indexOf('.') > email.indexOf('@')) { emailcon = true; } else { System.out.println("输入的邮箱地址无效!"); } // 输出校验的结果 if (filecon && emailcon) { System.out.println("作业提交成功!"); } else { System.out.println("作业提交失败!"); } }
************ 欢迎进入作业提交系统 ************ 请输入要提交的Java文件名称: answer.java 请输入要提交到的邮箱地址: answer@qq.com 作业提交成功!
************ 欢迎进入作业提交系统 ************ 请输入要提交的Java文件名称: abcjava 请输入要提交到的邮箱地址: abcjava@qqcom 输入的文件名无效! 输入的邮箱地址无效! 作业提交失败!
index!=-1&&name.charAt(index+1)='j' &&name.charAt(index+2)='a' &&name.charAt(index+3)='v' &&name.charAt(index+4)='a'
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码