gpt4 book ai didi

java - 如何将某个字符放在某个字符串之前?

转载 作者:行者123 更新时间:2023-11-29 09:00:00 25 4
gpt4 key购买 nike

我想做的是将 @ 字符放在域之前。我也在尝试弄清楚如何将域设置为最后 4 个字符(例如 .com)。我该怎么做呢?任何帮助,将不胜感激。

我已经在这个 link 上列出了我的工作.

链接中的代码:

import java.util.Scanner;

public class Username {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);

System.out.println("Must be between 8 and 20 characters.");
System.out.println("Must contain at least one uppercase and lowercase letter.");
System.out.println("Must contain at least one digit. ");
System.out.println("Must contain a special character ] [ ? / < ~ # ! $ % ^ & * ( ) + = } | : ; , > { ");
System.out.println("Must contain @ before the domain");
System.out.println("The only acceptable domains are .com .edu .org .mil .gov .net");
System.out.println("\\n____Please enter your username to access the page. Follow the rules above.____ ");

String input = keyboard.nextLine();
while ((input.length() < 8) || (input.length() > 20))
{
System.out.println("Error! Your input is not valid.");
System.out.println("Please try again.");
keyboard.nextLine();
}

for (int i = 0; i <= input.length(); i++)
{
if(Character.isUpperCase(input.charAt(i)))
{
break;
}
else
{
if(i == input.length())
{
System.out.println("Error: Try again");
input = keyboard.nextLine();
}
}
}

for (int i = 0; i <= input.length(); i++)
{
if(Character.isLowerCase(input.charAt(i)))
{
break;
}
else
{
if(i == input.length())
{
System.out.println("Try again");
input = keyboard.nextLine();
}
}
}

char [] numbers= {\'0\',\'1\',\'2\',\'3\', \'4\',\'5\',\'6\',\'7\',\'8\',\'9\'};
char[] inputArray = input.toCharArray();

for (int i = 0; i < inputArray.length; i++)
{
for (int j = 0; j < numbers.length; j++)
{
if (inputArray[i]== numbers[j])
{
i=inputArray.length;
j=numbers.length;

}
else
{
if(i == inputArray.length-1 && j== numbers.length-1)
{
System.out.println("Try again");
input = keyboard.nextLine();


}
}
}




char [] SpecialCharacter = {\']\',\'[\',\'?\',\'/\',\'<\',\'~\',\'#\',\'.\',\'!\',\'$\',\'%\',\'^\',\'&\',\'*\',\'(\',\')\',\'+\',\'=\',\'}\',\'|\',\'>\',\'{\' };
char[] inputArray2 = input.toCharArray();

for (int k = 0; k < inputArray2.length; k++)
{
for (int l = 0; l < SpecialCharacter.length; l++)
{
if (inputArray2[k]== SpecialCharacter[l])
{
k=inputArray2.length;
l=SpecialCharacter.length;
}
else
{
if(k == inputArray2.length-1 && l == SpecialCharacter.length-1)
{
System.out.println("No...Try Again");
input = keyboard.nextLine();
}
}
}

String domain1 = ".com";
String domain2 = ".edu";
String domain3 = ".org";
String domain4 = ".mil";
String domain5 = ".gov";
String domain6 = ".net";


}
}
}
}

最佳答案

如果您只需要验证用户是否输入了有效的电子邮件,那么您应该使用正则表达式,因为它使它更容易和快速。

    import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailValidator {

private Pattern pattern;
private Matcher matcher;

private static final String EMAIL_PATTERN =
"^(?=.*[0-9]*[a-z]*[A-Z])(?=.*[a-zA-Z])([a-zA-Z0-9]{8,20}+@[A-Za-z0-9]+.(com|org|edu|mil|net))$";

public EmailValidator() {
pattern = Pattern.compile(EMAIL_PATTERN);
}

/**
* Validate hex with regular expression
*
* @param hex
* hex for validation
* @return true valid hex, false invalid hex
*/
public boolean validate(final String hex) {

matcher = pattern.matcher(hex);
return matcher.matches();

}
}

希望对您有所帮助。 =)

关于java - 如何将某个字符放在某个字符串之前?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18036757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com