如何检查 String[Java] 中的特殊字符?
How do I check for special characters in a String[Java]?
我正在制作一款主机游戏..我希望玩家能够选择自己的用户名。拥有像 .-|\12b-}| 这样的用户名真的很奇怪。我希望能够检查用户名中是否包含“特殊字符”。如果玩家在 his/her 用户名中有特殊字符,我希望系统打印出一条消息(类似于:请更改您的用户名)。我不希望代码只是 个字母。
这是一个例子(如果你有答案,请按照这个):
import java.util.Scanner;
class WhosebugExample {
public static void main(String[] args) {
System.out.println("Welcome New Player! What will your name be?");
Scanner userInteraction = new Scanner(System.in);
String userInput = userInteraction.nextLine();
System.out.println("Welcome " + userInput + "!");
}
}
输出:
你明白为什么这会很奇怪了..
我希望能够扫描:
String userInput = userInteraction.nextLine();
对于任何奇怪的字符。 我将如何扫描字符串?
已编辑为具有正确的 java 代码
您可以通过使用对 return 的 int-asted charAt
调用来检查字符串中的每个字母是什么 # 该字符是 ASCII [参见此处] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt )
public static boolean isAlphaNumeric(String str) {
int code, i, len;
len = str.length();
for (i = 0; i < len; i++) {
code = (int) str.charAt(i);
if (!(code > 47 && code < 58) && // numeric (0-9)
!(code > 64 && code < 91) && // upper alpha (A-Z)
!(code > 96 && code < 123)) { // lower alpha (a-z)
return false;
}
}
return true;
};
public static void main(String[] args) {
String userInput = userInteraction.nextLine();
if(isAlphaNumeric(userInput )){
System.out.println("Welcome New Player! What will your name be?");
Scanner userInteraction = new Scanner(System.in);
String userInput = userInteraction.nextLine();
System.out.println("Welcome " + userInput + "!");
}
else{
System.out.println("Change your name from" + userInput + "!");
}}
试试这个。如果提供的不是字母,它会继续提示输入用户名。不允许使用像 Battle101 或 ~Hello~ 这样的用户名。
Scanner userInteraction = new Scanner(System.in);
String userInput = "";
while (true) {
System.out.println("Welcome New Player! What will your name be?");
userInput = userInteraction.nextLine();
if (userInput.matches("[a-zA-Z]+")) { // check that the input only contains letters
// okay so exit loop
break;
}
// else explain the problem and reprompt.
System.out.println("Only alphabetic characters are permitted.\nPlease try again.");
}
System.out.println("Welcome " + userInput + "!");
我正在制作一款主机游戏..我希望玩家能够选择自己的用户名。拥有像 .-|\12b-}| 这样的用户名真的很奇怪。我希望能够检查用户名中是否包含“特殊字符”。如果玩家在 his/her 用户名中有特殊字符,我希望系统打印出一条消息(类似于:请更改您的用户名)。我不希望代码只是
这是一个例子(如果你有答案,请按照这个):
import java.util.Scanner;
class WhosebugExample {
public static void main(String[] args) {
System.out.println("Welcome New Player! What will your name be?");
Scanner userInteraction = new Scanner(System.in);
String userInput = userInteraction.nextLine();
System.out.println("Welcome " + userInput + "!");
}
}
输出:
你明白为什么这会很奇怪了..
我希望能够扫描:
String userInput = userInteraction.nextLine();
对于任何奇怪的字符。 我将如何扫描字符串?
已编辑为具有正确的 java 代码
您可以通过使用对 return 的 int-asted charAt
调用来检查字符串中的每个字母是什么 # 该字符是 ASCII [参见此处] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt )
public static boolean isAlphaNumeric(String str) {
int code, i, len;
len = str.length();
for (i = 0; i < len; i++) {
code = (int) str.charAt(i);
if (!(code > 47 && code < 58) && // numeric (0-9)
!(code > 64 && code < 91) && // upper alpha (A-Z)
!(code > 96 && code < 123)) { // lower alpha (a-z)
return false;
}
}
return true;
};
public static void main(String[] args) {
String userInput = userInteraction.nextLine();
if(isAlphaNumeric(userInput )){
System.out.println("Welcome New Player! What will your name be?");
Scanner userInteraction = new Scanner(System.in);
String userInput = userInteraction.nextLine();
System.out.println("Welcome " + userInput + "!");
}
else{
System.out.println("Change your name from" + userInput + "!");
}}
试试这个。如果提供的不是字母,它会继续提示输入用户名。不允许使用像 Battle101 或 ~Hello~ 这样的用户名。
Scanner userInteraction = new Scanner(System.in);
String userInput = "";
while (true) {
System.out.println("Welcome New Player! What will your name be?");
userInput = userInteraction.nextLine();
if (userInput.matches("[a-zA-Z]+")) { // check that the input only contains letters
// okay so exit loop
break;
}
// else explain the problem and reprompt.
System.out.println("Only alphabetic characters are permitted.\nPlease try again.");
}
System.out.println("Welcome " + userInput + "!");