字符串处理_验证文件名和邮箱地址是否合法

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("提交失败!");
}

}
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值