【字符串中处理类String的使用】

解析一个邮箱地址是否合法,如果合法则打印出用户名部分和该邮箱所属的网站域名,如果邮箱地址不合法则显示不合法的原因

1 提示:邮箱地址不合法的因素:
1.1 邮箱地址中不包含@或. 
1.2 邮箱地址中含有多了@或. 
1.3 邮箱地址中.出现在@的前面
1.4 用户名里有其他字符

2 实现步骤:
2.1 创建一个类

package aaaa;

import java.util.Scanner;

public class J邮箱地址 {
	public static void main(String[] args) {
		System.out.println("请输入一个邮箱地址: ");
		Scanner sc=new Scanner(System.in);
		String x=sc.next();
		if(Mail(x)==true) {
			
		}else {
			System.out.println("不合法");
		}
	}
	public static boolean Mail(String x) {
		boolean b=true;
		if(x.indexOf('@')==-1||x.indexOf('.')==-1) {
			System.out.println("邮箱地址中不包含@或.");
			return false;
		}
		if(x.indexOf('@')!=x.lastIndexOf('@')||x.indexOf('.')!=x.lastIndexOf('.')) {
			System.out.println("邮箱地址中含有多了@或.");
			return false;
		}
		if(x.lastIndexOf('@')>x.lastIndexOf('.')) {
			System.out.println("邮箱地址中.出现在@的前面");
			return false;
		}
		for(int i=0;i<x.indexOf('@');i++) {
			char c=x.charAt(i);
			if((c>=48&&c<=57)||(c>=65)&&(c<=90)||(c>=97)&&(c<=122)) {
				
			}
			else {
				System.out.println("用户名里有其他字符");
				return false;
			}
		}
		String[] arr=x.split("@");
		System.out.println("用户名为:"+arr[0]);
		System.out.println("该邮箱所属的网站域名为:"+arr[1]);
		return true;
	}

}

结果截图:
在这里插入图片描述

int indexOf/lastIndexOf(char)
返回指定字符在此字符串中第一次/最后一次出现处的索引。
char charAt(int)
返回指定索引处的 char 值
String[] split(String regex)
根据给定正则表达式的匹配拆分此字符串。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值