我正在尝试读取一个配置文件,我想在一些代数运算中使用该属性值。所以我需要将 prop.getProperty(String str)
返回的字符串转换成一个整数。
我试过使用以下方法转换它:
1.)
Integer value = null;
String string = getProperty(key);
if (string != null)
value = new Integer(string);
return value;
2.)
String noofdivs = prop.getProperty("NO_OF_INITIAL_DIVS");
Integer noOfInitialDivs = Integer.valueOf(noofdivs);
3.)
String xyz = prop.getProperty("NO_OF_LINES_IN_A_DIV");
Integer noOfLinesInDiv = Integer.getInteger(xyz);
但它们都不起作用。
谁能帮我解决这个问题?
最佳答案
int value = Integer.parseInt(string);
然后您可以检查 NumberFormatException
以查看它是否被正确解析。
关于java - 如何使用 java 将 prop.getProperty() 返回的字符串转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16891940/