Java 验证输入是一个数字,在一系列数字或特定字符中
我已经编写了以下代码来检查输入值是数字还是字符'R'或'R' 我还需要检查它是否是一个数字,它是否大于0且小于数组长度 在现有条件下,是否有可能在同一条线路上完成所有这些工作Java 验证输入是一个数字,在一系列数字或特定字符中,java,validation,input,Java,Validation,Input,我已经编写了以下代码来检查输入值是数字还是字符'R'或'R' 我还需要检查它是否是一个数字,它是否大于0且小于数组长度 在现有条件下,是否有可能在同一条线路上完成所有这些工作 int array_length = menu.length; while (!input.hasNextInt() && !input.hasNext("[rR]")) { System.out.println("Invalid option entered. Please try again."
int array_length = menu.length;
while (!input.hasNextInt() && !input.hasNext("[rR]")) {
System.out.println("Invalid option entered. Please try again.");
System.out.println();
System.out.print("Select option > ");
input.next();
}
我并没有在这里优化字符串比较,但基本上这就是您可能需要的
Scanner input = new Scanner(System.in);
int array_length = 5;
String value;
while (!"R".equals(value = input.next()) &&
!"r".equals(value) &&
!(value.matches("[0-9]+") && Integer.parseInt(value) >= 0 && Integer.parseInt(value) < array_length )) {
System.out.println("Invalid option entered. Please try again.");
System.out.println();
System.out.print("Select option > ");
}
System.out.println("Thanks!");
扫描仪输入=新扫描仪(System.in);
int数组_长度=5;
字符串值;
while(!“R”.equals(value=input.next())&&
!“r”。等于(值)&&
!(value.matches(“[0-9]+”&&Integer.parseInt(value)>=0&&Integer.parseInt(value)”;
}
System.out.println(“谢谢!”);
这里我没有优化字符串比较,但基本上这就是您可能需要的
Scanner input = new Scanner(System.in);
int array_length = 5;
String value;
while (!"R".equals(value = input.next()) &&
!"r".equals(value) &&
!(value.matches("[0-9]+") && Integer.parseInt(value) >= 0 && Integer.parseInt(value) < array_length )) {
System.out.println("Invalid option entered. Please try again.");
System.out.println();
System.out.print("Select option > ");
}
System.out.println("Thanks!");
扫描仪输入=新扫描仪(System.in);
int数组_长度=5;
字符串值;
while(!“R”.equals(value=input.next())&&
!“r”。等于(值)&&
!(value.matches(“[0-9]+”&&Integer.parseInt(value)>=0&&Integer.parseInt(value)”;
}
System.out.println(“谢谢!”);
我想对您的案例使用正则表达式
int size = 3; // replace by your menu length
String input = "r";
String regEx = String.format("^(r|R|[1-9][0-9]*{1,%d}$)", size);
Pattern p = Pattern.compile(regEx, Pattern.DOTALL);
Matcher m = p.matcher(input);
while (m.find()) {
// your logic here
// if it comes to this point, it means
// the input is a number or
// the input is r or
// the input is R and
// the input must be greater than 0
// the input must be in range is 'size'
System.out.println("Qualified with all above criteria");
}
我想用正则表达式来表示你的情况
int size = 3; // replace by your menu length
String input = "r";
String regEx = String.format("^(r|R|[1-9][0-9]*{1,%d}$)", size);
Pattern p = Pattern.compile(regEx, Pattern.DOTALL);
Matcher m = p.matcher(input);
while (m.find()) {
// your logic here
// if it comes to this point, it means
// the input is a number or
// the input is r or
// the input is R and
// the input must be greater than 0
// the input must be in range is 'size'
System.out.println("Qualified with all above criteria");
}
当然,不会使其非常可读/可维护,但如何获取条件中输入的值?@AsaCarter在循环之前声明一个整数(即
int-inputNumber;
),然后在条件内分配它:(inputNumber=input.nextInt())当然,如果在循环前声明一个整数(即int-inputNumber;
),然后在条件内分配它:(inputNumber=input.nextInt())OP要求也读取相同条件内的输入。另外,您不检查输入是否真的是整数就执行Integer.parseInt(value)
,如果没有要解析的整数,这将引发NumberFormatException
。感谢您的更正,我添加了正确的验证。OP要求在相同条件下也读取输入。另外,您可以执行Integer.parseInt(value)
操作,而不检查输入是否真的是整数,如果没有要解析的整数,则会引发NumberFormatException
。感谢您的更正,我添加了正确的验证。