如何在某个字符串之前放置某个字符?

时间:2013-08-03 19:59:53

标签: java string

我要做的是将@字符放在域名之前。我还试图找出如何将域名作为最后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";


                         }
        }
        }
}

2 个答案:

答案 0 :(得分:0)

我会把它放在这里......

在需要的地方插入代码部分。

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.____ ");

            boolean isOK = false;
            String input=new String();

            while(!isOK){
                input=keyboard.nextLine();

                if(input.length() < 20 || input.length() > 8) //length check
                {
                    isOK=true;
                }
                else
                {
                    isOK=false;
                    continue;
                }

                if(input.contains('@')) //check domain
                {
                    isOK=true;                      
                }
                else
                {
                    isOK=false;
                    System.out.println("No @ before domain!");
                    continue;
                }

                String[] tokens = input.split("@");
                String domain=tokens[1];
                String username=tokens[0];

                //check if contains digit
                ...

                //check uppercase and lowercase in username
                ...
                //check special character in username
                ...

                //split domain
                tokens = domain.split(".");
                String domainEnding = tokens[1];

                //check if the input domain endings are allowed
                ...


            }
    }
}

因此,输入部分错误只需使用isOK=false; continue;

BTW:你为什么在特殊字符和数字数组中使用\'? 您只能使用'1''@'

答案 1 :(得分:0)

如果您只需要验证用户是否输入了有效的电子邮件,那么您应该使用常规版本,这样可以更加轻松快捷。

    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();

}
   } 
希望这会对你有所帮助。 =)