简体   繁体   English

获得一系列结果的最佳方法是什么?

[英]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.

相关问题 有没有办法在 DynamoDB 查询中获得特定范围的结果? - Is there a way to get a specific range of results in a DynamoDB query? 验证带小数的值是否在定义的范围内的最佳方法是什么 - What is the best way to validate if a value with decimals is in a defined range 代表给定范围内所有数字的最佳方法是什么? (一些限制) - What is the best way to represent all numbers in a given range? (some restrictions) 在Java中将泛型范围实现为函数参数的最佳方法是什么? - What is the best way to implement a generic range as a function argument in Java? 在哈希图中获取所有元素的最佳方法是什么? - What is the best way to get all elements in a hashmap? 没有HATEOAS,获得响应的最佳方法是什么? - What is the best way to get response without HATEOAS? 在Clojure中获取日期和时间的最佳方式是什么? - What is the best way to get date and time in Clojure? 在列表视图中选择项目的最佳方法是什么? - What is the best way to get the item selected in the listview? 获得Cassandra Writes背压的最佳方法是什么? - What is the best way to get backpressure for Cassandra Writes? 在Java中使用Hibernate的DetachedCriteria来限制结果的最佳方法是什么? - What is the best way to limit results using a DetachedCriteria of Hibernate in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM