Picocli – java.lang.NumberFormatException

huangapple go评论74阅读模式
英文:

Picocli - java.lang.NumberFormatException

问题

@Parameters(index = "0")
private Double min_c_re;

@Parameters(index = "1")
private Double min_c_im;

@Parameters(index = "2")
private Double max_c_re;

@Parameters(index = "3")
private Double max_c_im;

@Parameters(index = "4")
private Integer max_n;

@Parameters(index = "5")
private Integer width;

@Parameters(index = "6")
private Integer height;

@Parameters(index = "7")
private Integer divisions;

@Parameters(index = "8", arity = "1..*")
private List<URL> hosts;

@Override
public void run() {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    List<SubDivider.SubDivision> subDividedImages = new SubDivider(divisions, divisions).divide(image);

    ExecutorService threadPool = Executors.newCachedThreadPool();
    double realRange = max_c_re - min_c_re;
    double imaginaryRange = max_c_im - min_c_im;

    for (int i = 0; i < subDividedImages.size(); i++) {
        SubDivider.SubDivision subDivision = subDividedImages.get(i);
        BufferedImage subDividedImage = subDivision.getImage();
        URL host = hosts.get(i % hosts.size());

        double xPercent = subDivision.getOriginalMinX() / (double) width;
        double yPercent = subDivision.getOriginalMinY() / (double) height;
        double widthPercent = subDividedImage.getWidth() / (double) width;
        double heightPercent = subDividedImage.getHeight() / (double) height;

        String resource = new StringJoiner("/")
                             .add("/mandelbrot")
                             .add(Double.toString(min_c_re + realRange * xPercent))
                             .add(Double.toString(min_c_im + imaginaryRange * yPercent))
                             .add(Double.toString(min_c_re + realRange * xPercent + realRange * widthPercent))
                             .add(Double.toString(min_c_im + imaginaryRange * yPercent + imaginaryRange * heightPercent))
                             .add(Integer.toString(subDividedImage.getWidth()))
                             .add(Integer.toString(subDividedImage.getHeight()))
                             .add(Integer.toString(max_n))
                             .toString();

        URL url;
        try {
            url = new URL(host, resource);
        } catch (MalformedURLException exception) {
            System.err.println("Exception: " +
                               exception.getMessage());
            return;
        }

        threadPool.submit(new SubDivisionGenerator(url, subDividedImage));
    }

    try {
        threadPool.shutdown();
        threadPool.awaitTermination(1, TimeUnit.HOURS);
    } catch (InterruptedException exception) {
        System.err.println("Exception: "
                           + exception.getMessage());
        return;
    }

    File file = new File("output.pgm");
    HashMap<String, Object> params = new HashMap<>();

    try {
        Imaging.writeImage(image, file, ImageFormats.PGM, params);
    } catch (ImageWriteException | IOException exception) {
        System.err.println("Exception: "
                           + exception.getMessage());
        return;
    }
}

当我运行命令 java -jar target/cli-1.0.0.jar -1 -1.5 2 1.5 1024 10000 10000 4 http://localhost:8080 时,我得到以下异常:

Could not convert 'https://localhost:8080' to Integer for positional parameter at index 6 (<height>): java.lang.NumberFormatException: For input string: "https://localhost:8080"

使用的 picocli 版本为 2.3.0。

英文:
@Parameters(index = &quot;0&quot;)
private Double min_c_re;
@Parameters(index = &quot;1&quot;)
private Double min_c_im;
@Parameters(index = &quot;2&quot;)
private Double max_c_re;
@Parameters(index = &quot;3&quot;)
private Double max_c_im;
@Parameters(index = &quot;4&quot;)
private Integer max_n;
@Parameters(index = &quot;5&quot;)
private Integer width;
@Parameters(index = &quot;6&quot;)
private Integer height;
@Parameters(index = &quot;7&quot;)
private Integer divisions;
@Parameters(index = &quot;8&quot;, arity = &quot;1..*&quot;)
private List&lt;URL&gt; hosts;
@Override
public void run() {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
List&lt;SubDivider.SubDivision&gt; subDividedImages = new SubDivider(divisions, divisions).divide(image);
ExecutorService threadPool = Executors.newCachedThreadPool();
double realRange = max_c_re - min_c_re;
double imaginaryRange = max_c_im - min_c_im;
for (int i = 0; i &lt; subDividedImages.size(); i++) {
SubDivider.SubDivision subDivision = subDividedImages.get(i);
BufferedImage subDividedImage = subDivision.getImage();
URL host = hosts.get(i % hosts.size());
double xPercent = subDivision.getOriginalMinX() / (double) width;
double yPercent = subDivision.getOriginalMinY() / (double) height;
double widthPercent = subDividedImage.getWidth() / (double) width;
double heightPercent = subDividedImage.getHeight() / (double) height;
String resource = new StringJoiner(&quot;/&quot;)
.add(&quot;/mandelbrot&quot;)
.add(Double.toString(min_c_re + realRange * xPercent))
.add(Double.toString(min_c_im + imaginaryRange * yPercent))
.add(Double.toString(min_c_re + realRange * xPercent + realRange * widthPercent))
.add(Double.toString(min_c_im + imaginaryRange * yPercent + imaginaryRange * heightPercent))
.add(Integer.toString(subDividedImage.getWidth()))
.add(Integer.toString(subDividedImage.getHeight()))
.add(Integer.toString(max_n))
.toString();
URL url;
try {
url = new URL(host, resource);
} catch (MalformedURLException exception) {
System.err.println(&quot;Exception: &quot; +
exception.getMessage());
return;
}
threadPool.submit(new SubDivisionGenerator(url, subDividedImage));
}
try {
threadPool.shutdown();
threadPool.awaitTermination(1, TimeUnit.HOURS);
} catch (InterruptedException exception) {
System.err.println(&quot;Exception: &quot;
+ exception.getMessage());
return;
}
File file = new File(&quot;output.pgm&quot;);
HashMap&lt;String, Object&gt; params = new HashMap&lt;&gt;();
try {
Imaging.writeImage(image, file, ImageFormats.PGM, params);
} catch (ImageWriteException | IOException exception) {
System.err.println(&quot;Exception: &quot;
+ exception.getMessage());
return;
}
}

When I run the command java -jar target/cli-1.0.0.jar -1 -1.5 2 1.5 1024 10000 10000 4 http://localhost:8080

I get this exception:

Could not convert &#39;https://localhost:8080&#39; to Integer for positional parameter at index 6 (&lt;height&gt;): java.lang.NumberFormatException: For input string: &quot;https://localhost:8080&quot;

picocli version 2.3.0 is used

答案1

得分: 5

尝试双短线 https://picocli.info/#_double_dash:

java -jar target/cli-1.0.0.jar -- -1 -1.5 2 1.5 1024 10000 10000 4 http://localhost:8080

负数不能直接传递到命令行。它们没有被识别为位置参数,因此它在说您的 URL 处于第 6 位置。

英文:

Try double dash https://picocli.info/#_double_dash:

java -jar target/cli-1.0.0.jar -- -1 -1.5 2 1.5 1024 10000 10000 4 http://localhost:8080

negative numbers can not be passed directly on the command line. They have not been recognized as positional parameters, therefore it is saying that your url is at position 6

答案2

得分: 3

负数中的破折号使它们看起来像是选项。尝试使用以下标志:

CommandLine.setUnmatchedOptionsArePositionalParams(true)
英文:

The dash in the negative numbers make them look like options. Try using the flag:

CommandLine.setUnmatchedOptionsArePositionalParams(true)

huangapple
  • 本文由 发表于 2020年4月6日 17:29:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/61056691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定