0

I have implemented some service which works fine. The service has the following property:

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

I get the value of the port property as follow:

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

port in this case results 8080. Now I would like to convert this String to Integer. I tried the following:

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

Both results null :(

What did I do worng?

Max_Salah
  • 2,407
  • 11
  • 39
  • 68

3 Answers3

3

Consider using PropertiesUtil, an utility class created exactly for this case:

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

The second parameter is the default value.

Tomek Rękawek
  • 9,204
  • 2
  • 27
  • 43
1

If the port has a value and is not null then try:

int portInt = Integer.valueOf(port.trim());
Henrik
  • 1,797
  • 4
  • 22
  • 46
0

Try if it work:

int portInt = Integer.parseInt(port+""); // check if the port value is coming or not before parse it.
int portInt2 = Integer.getInteger(port+"");
Ashish Ratan
  • 2,838
  • 1
  • 24
  • 50
  • agree man, but it may be a case, same kind of problem was happening with me, with string parsing... so i try this way and it was working – Ashish Ratan Feb 26 '14 at 10:03
  • This will yield a slightly different exception (if we have an exception at all, OP did not tell us), but not solve the problem. – Gyro Gearless Feb 26 '14 at 10:11