字符串无法用空格识别或为空

时间:2014-07-25 01:41:36

标签: java

在我开始讨论我的问题之前,我真的(并且我的意思是真的)在我的解决方案上研究了几个小时,但我找不到它或清楚地理解它;在我的第二个职业生涯问题被投票后,很多,我对发布我目前的投资组合免费长矛项目代码持怀疑态度。如果我看起来像一个初学者,那是因为我;我也对目前的进展感到尴尬。

但足够了:我正在创建一个潜在分销公司的项目,我想对字符串添加限制。如何在找到空格/空字符串之前阻止代码继续?

    public class AddOrder {
static Scanner input = new Scanner (System.in);
//Blocking special character input

 private static CharSequence s1 = "!";
 private static CharSequence s2 = "@";
 private static CharSequence s3 = "#";
 private static CharSequence s4 = "$";
 private static CharSequence s5 = "%";
 private static CharSequence s6 = "^";
 private static CharSequence s7 = "&";
 private static CharSequence s8 = "*";
 private static CharSequence s9 = "(";
 private static CharSequence s10 = ")";
 private static CharSequence s11 = "_";
 private static CharSequence s12 = "=";
 private static CharSequence s13 = "+";
 private static CharSequence s14 = "[";
 private static CharSequence s15 = "]";
 private static CharSequence s16 = "{";
 private static CharSequence s17 = "}";
 private static CharSequence s18 = ";";
 private static CharSequence s19 = ":";
 private static CharSequence s20 = "'";
 private static CharSequence s21 = "?";
 private static CharSequence s22 = "<";
 private static CharSequence s23 = ">";
 private static CharSequence s24 = "/";
 private static CharSequence s25 = "`";
 private static CharSequence s26 = "~";
 private static CharSequence s27 = " ";
 private static CharSequence s28 = ".";

 //Blocking special character input


public static void informationPrompt(){

    System.out.println("Please insert following information:");
    System.out.println("Last Name, First Name, Middle Initial");
    System.out.println("Street Address");
    System.out.println("Zip code, City, State, Country");
}
public static void setLastname(){
    System.out.println("Please enter: Last Name");
    String lastName = input.nextLine();

    for (int i = 0; i < lastName.length(); i++){
        if (lastName.length() > 50 || lastName.isEmpty() || Character.isWhitespace(i) || Character.isDigit(lastName.charAt(i)) || lastName.contains(s1)||  lastName.contains(s2)
        || lastName.contains(s3) || lastName.contains(s4) || lastName.contains(s5) || lastName.contains(s6) || lastName.contains(s7) || lastName.contains(s8)
        || lastName.contains(s9) || lastName.contains(s10) || lastName.contains(s11) || lastName.contains(s12) || lastName.contains(s13) || lastName.contains(s14)
        || lastName.contains(s15) || lastName.contains(s16) || lastName.contains(s17) || lastName.contains(s18) || lastName.contains(s19) || lastName.contains(s20)
        || lastName.contains(s21) || lastName.contains(s22) || lastName.contains(s23) || lastName.contains(s24) || lastName.contains(s25) || lastName.contains(s26)){
            System.out.println("Incorrect input. Either:\n"
                    + "1: Fill in last name.\n"
                    + "2: Numerics are not allowed; please remove numbers\n"
                    + "3: Name is longer than 50 characters; please shorten.\n"
                    + "4: No special characters are allowed (e.g: !,@,#,$, etc.);"
                    + "please remove special characters\n"
                    + "5: No spaces (whitespaces) are allowed; Please fill in without spaces.");                    
            setLastname();
        }

        }
    }

我知道这是一团糟。任何意见/建议将不胜感激。

2 个答案:

答案 0 :(得分:1)

如果您查看ascii table,您会看到您想要的输入A-Z或a-z在65-90和97-122的连续编号中运行

有时候对允许的内容进行处理比不允许的更好。

其他人建议使用正则表达式String expression = "^[a-zA-Z\\s]+";,它适用于非常基本的名称。

长度和空的检查只需要进行一次。

答案 1 :(得分:0)

我解决了自己的问题。我只是将.isEmpty()方法与我称之为异常的丑陋的mosh坑分开,并使用了与i和lastName字符串相关的Character类(Character.isWhitespace(lastName.charAt(i)):

    public static void setLastname(){
    System.out.println("Please enter: Last Name");
    String lastName = input.nextLine();
    if (lastName.isEmpty()){
    System.out.println("Incorrect input. Either:\n"
            + "1: Fill in last name.\n"
            + "2: Numerics are not allowed; please remove numbers\n"
            + "3: Name is longer than 50 characters; please shorten.\n"
            + "4: No special characters are allowed (e.g: !,@,#,$, etc.);"
            + "please remove special characters\n"
            + "5: No spaces (whitespaces) are allowed; Please fill in without spaces.");                    
    setLastname();
    }
    for (int i = 0; i < lastName.length(); i++){
        if (lastName.length() > 50 || Character.isWhitespace(lastName.charAt(i)) || Character.isDigit(lastName.charAt(i)) || lastName.contains(s1)||  lastName.contains(s2)
        || lastName.contains(s3) || lastName.contains(s4) || lastName.contains(s5) || lastName.contains(s6) || lastName.contains(s7) || lastName.contains(s8)
        || lastName.contains(s9) || lastName.contains(s10) || lastName.contains(s11) || lastName.contains(s12) || lastName.contains(s13) || lastName.contains(s14)
        || lastName.contains(s15) || lastName.contains(s16) || lastName.contains(s17) || lastName.contains(s18) || lastName.contains(s19) || lastName.contains(s20)
        || lastName.contains(s21) || lastName.contains(s22) || lastName.contains(s23) || lastName.contains(s24) || lastName.contains(s25) || lastName.contains(s26)) {
            System.out.println("Incorrect input. Either:\n"
                    + "1: Fill in last name.\n"
                    + "2: Numerics are not allowed; please remove numbers\n"
                    + "3: Name is longer than 50 characters; please shorten.\n"
                    + "4: No special characters are allowed (e.g: !,@,#,$, etc.);"
                    + "please remove special characters\n"
                    + "5: No spaces (whitespaces) are allowed; Please fill in without spaces.");                    
            setLastname();
        }

        }
    }

现在看起来比以前更糟糕,更“新鲜”。我试图向其他人和我自己证明,电气工程专业的学生可以跟上大CS男孩和女孩。谢谢大家的意见。我对本周晚些时候要检查我的代码的人感到遗憾!笑!