import java.util.Scanner;
class Verify{
boolean fileCorrect = false; //标识文件名是否合法
boolean mailCorrect = false; //标识邮箱地址是否合法
//检查java文件名是否合法
public boolean checkFile(String fileName){
int index = fileName.lastIndexOf('.');//找到.的位置,.不存在时会返回-1
//(注意:lastIndexOf后两个单词首字母为大写)
String fileFormat = fileName.substring(index+1, fileName.length());//截取文件格式名(.后的字符串即为文件格式名)
if(index!=-1 && index!=0 && fileFormat.equalsIgnoreCase("java")){
fileCorrect = true;
}
else {
System.out.println("非法的文件格式!");
}
return fileCorrect;
}
//检查邮箱地址是否合法
public boolean checkMail(String mail){
if (mail.indexOf('.')!=-1 && mail.indexOf('.')!=0 &&
mail.lastIndexOf('.')>mail.indexOf('@')) {
mailCorrect = true;
}
else {
System.out.println("非法的邮件地址!");
}
return mailCorrect;
}
}
public class Dm{
public static void main(String[] args){
System.out.println("---欢迎使用作业提交系统---");
Scanner input = new Scanner(System.in);
System.out.print("请输入文件名:");
String fileName = input.next();
System.out.print("请输入你的邮箱:");
String mail = input.next();
//创建检查对象check
Verify check = new Verify();
if(check.checkFile(fileName) && check.checkMail(mail))
System.out.println("提交成功!");
else {
System.out.println("提交失败!");
}
}
}
字符串处理_验证文件名和邮箱地址是否合法
最新推荐文章于 2018-04-16 20:08:40 发布