将值转换为整数

14

如何获取对象的值并将其存储在整数变量中? 在字符串中我这样做:

String k = s_extend.getValue().toString();

那 int 类型怎么处理?


你听说过 Integer.parseInt() 吗? - Baby
1个回答

13

你可以使用 Integer.valueOf() 或者 Integer.parseInt();除非你的问题还有其他需要解决的地方 -

public static void main(String[] args) {
  String str = "1";
  int a = Integer.valueOf(str);                 // java.lang.Integer return(ed)
  int b = Integer.parseInt(str);                // primitive (int) return(ed)
  System.out.printf("a = %d, b = %d\n", a, b);
}

在我的电脑上运行这个命令的结果是:

a = 1, b = 1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接