gpt4 book ai didi

java - 无法从字符串转换为整数

转载 作者:行者123 更新时间:2023-12-01 18:35:54 24 4
gpt4 key购买 nike

我已经实现了一些运行良好的服务。该服务具有以下属性:

 @Property(name = MyService.PROXY_PORT, label = "Proxy Port", description = "Proxy Port", value= "8080")
private static final String PROXY_PORT = "proxy.port";

我获取 port 属性的值如下:

..
...
properties = context.getProperties();
String port = properties.get(PROXY_PORT).toString();
...

port 在本例中结果为 8080。现在我想将此字符串转换为整数。我尝试了以下方法:

int portInt = Integer.parseInt(port);
int portInt2 = Integer.getInteger(port);

两个结果都为空:(

我做了什么?

最佳答案

考虑使用PropertiesUtil ,一个专门为这种情况创建的实用程序类:

int port = PropertiesUtil.toInteger(properties.get(PROXY_PORT), 8080);

第二个参数是默认值。

关于java - 无法从字符串转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22037736/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com