簡體   English   中英

無法從String轉換為Integer

[英]cannot convert from String to Integer

我已經實施了一些工作正常的服務。 該服務具有以下屬性:

 @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 現在,我想將此String轉換為Integer。 我嘗試了以下方法:

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

兩個結果都為空:(

我做了什么?

考慮使用PropertiesUtil ,它是專門為這種情況創建的實用程序類:

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

第二個參數是默認值。

如果端口具有值並且不為null,請嘗試:

int portInt = Integer.valueOf(port.trim());

嘗試是否有效:

int portInt = Integer.parseInt(port+""); // check if the port value is coming or not before parse it.
int portInt2 = Integer.getInteger(port+"");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM