[英]How do I make sure that the input is not null and that only one character is being entered?
public static boolean correctchar(char b) {
Scanner scan = new Scanner(System.in);
b = scan.next().charAt(0);
if (Character.toString(b).matches("^[a-zA-Z]") ) {
System.out.println("True");
return true;
} else {
System.out.println("False");
return false;
}
}
I have this method that checks whether the input is a letter in the alphabet or not, but I want to make sure that the input from the user is not null and that the user only enters one letter.我有这种方法可以检查输入是否是字母表中的字母,但我想确保用户的输入不是 null 并且用户只输入一个字母。 For example " A
" or " a
" is a correct char, the problem is if I enter " Abcdef
" then it is still true as the first letter is still a valid char.例如“ A
”或“ a
”是一个正确的字符,问题是如果我输入“ Abcdef
”那么它仍然是正确的,因为第一个字母仍然是一个有效的字符。 I want to make it so that the user can only enter one char, I think I've done that by using the scanner and charAt(0)
but is there a more efficient way to do it, and I'm also not sure how to make it so that the input isn't null.我想让它让用户只能输入一个字符,我想我已经通过使用扫描仪和charAt(0)
来做到这一点,但是有没有更有效的方法来做到这一点,我也不确定如何使输入不是null。
I've revised your code to do what you wanted:我已经修改了您的代码以执行您想要的操作:
public static boolean correctchar(char b) {
Scanner scan = new Scanner(System.in);
String input = scan.next();
// This checks if the input is null, is empty (i.e., "") or is bigger than one character
// If any of these conditions are fulfilled, then we return false.
if (input == null || input.length() != 1) {
return false;
}
b = input.charAt(0);
if (Character.toString(b).matches("[a-zA-Z]") ) {
System.out.println("True");
return true;
} else {
System.out.println("False");
return false;
}
}
EDIT编辑
Without scanner (see comments):没有扫描仪(见评论):
public static boolean correctchar(char b, String input) {
// This checks if the input is null, is empty (i.e., "") or is bigger than one character
// If any of these conditions are fulfilled, then we return false.
if (input == null || input.length() != 1) {
return false;
}
b = input.charAt(0);
if (Character.toString(b).matches("[a-zA-Z]") ) {
System.out.println("True");
return true;
} else {
System.out.println("False");
return false;
}
}
public static boolean correctChar() {
try (Scanner scan = new Scanner(System.in)) {
String input = null;
do {
input = scan.next();
if (input != null && input.length() == 1) {
boolean isCorrect = input.matches("[a-zA-Z]");
System.out.println(isCorrect ? "True" : "False");
return isCorrect;
} else {
System.out.println("Insert only one character");
}
} while (true);
}
}
I made couple of changes:我做了几个改变:
If invalid input ask user to enter again.如果输入无效,请用户重新输入。
Make sure to close the scanner scan.close()确保关闭扫描仪 scan.close()
Scanner scan = new Scanner(System.in); System.out.println("Please enter only one character: "); String input = scan.next(); while (null == input || input.isEmpty() || input.length() > 1) { System.out.println("Invaid Input, Please enter only one character: "); input = scan.next(); } scan.close(); if (Character.toString(input.charAt(0)).matches("^[a-zA-Z]")) { System.out.println("True"); return true; } else { System.out.println("False"); return false; }
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.