[英]What is the best way to get a range of results?
I have a program that can be run from the command line with a -R
to specify a range of results to return. 我有一个程序,可以从命令行运行
-R
以指定要返回的结果范围。 The -R
takes one argument of the form n:n
. -R
采用n:n
形式的一个参数。
Seeing as though n:n
could be 1-5 or 10000-500000, what is the best way of get the values on both sides of the :
? 眼看好像
n:n
可能是1-5或者10000-500000,什么是得到的两侧值的最佳方式:
?
After passing my arg following -R
to a method I started doing the following: 通过我下面的ARG后
-R
到我开始做下面的方法:
private int[] parseRangeResults(String range) {
int[] rangeResults = new int[2];
if(!(range.contains(":"))) {
throw new Exception("Invalid range syntax");
}
String[] numbers = range.split(":")
rangeResults[0] = Integer.parseInt(numbers[0]);
rangeResults[1] = Integer.parseInt(numbers[2]);
return rangeResults;
}
But I think this breaks down if someone puts special characters, or 1000:::::5000
, so what is the best way of handling this? 但是我认为如果有人放了特殊字符,或者
1000:::::5000
,这就会1000:::::5000
,那么处理这个问题的最佳方法是什么呢?
The best way to check the input is with regular expressions if this regex("\\\\d*-\\\\d*")
matches, then your output is correct. 如果此
regex("\\\\d*-\\\\d*")
匹配,则检查输入的最佳方法是使用正则表达式,然后输出正确。 this is the code for your function 这是你的功能的代码
String regex = "(\\d*):(\\d*)";
Pattern checkInput = Pattern.compile(regex);
Matcher matcherInput = checkInput.matcher(range);
if(matcherInput.matches()){
rangeResults[1] = Integer.parseInt(matcherInput.group(1));
rangeResults[2] = Integer.parseInt(matcherInput.group(2));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.