gpt4 book ai didi

java - 字符串无法识别为空格或空

转载 作者:太空宇宙 更新时间:2023-11-04 06:38:51 25 4
gpt4 key购买 nike

在开始讨论我的问题之前,我真的(我的意思是真的)研究了几个小时来研究我的解决方案,但我找不到它或清楚地理解它;在我的第二个职业问题被多次否决后,我对发布我当前的投资组合自由项目代码持怀疑态度。如果我看起来像个初学者,那是因为我就是;我也为自己目前的进步感到尴尬。

但是已经足够了:我正在创建一个潜在发行公司的项目,并且我想对字符串添加限制。在找到空格/空字符串之前如何防止代码继续运行?

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

}
}

我知道这很困惑。任何意见/建议将不胜感激。

最佳答案

如果你看一下 ascii table您将看到您想要的输入 A-Z 或 a-z 按 65-90 和 97-122 的顺序编号运行

有时,做允许的事情比做不允许的事情更好。

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

此外,长度和空的检查只需要完成一次。

关于java - 字符串无法识别为空格或空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24946748/

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