如何将String转换为int?
"1234" → 1234
如何将String转换为int?
"1234" → 1234
当前回答
对于Android开发者来说,以下是Kotlin的各种解决方案:
// Throws exception if number has bad form
val result1 = "1234".toInt()
// Will be null if number has bad form
val result2 = "1234"
.runCatching(String::toInt)
.getOrNull()
// Will be the given default if number has bad form
val result3 = "1234"
.runCatching(String::toInt)
.getOrDefault(0)
// Will be return of the else block if number has bad form
val result4 = "1234"
.runCatching(String::toInt)
.getOrElse {
// some code
// return an Int
}
其他回答
嗯,需要考虑的一个非常重要的点是Integer解析器抛出了Javadoc中所述的NumberFormatException。
int foo;
String StringThatCouldBeANumberOrNot = "26263Hello"; //will throw exception
String StringThatCouldBeANumberOrNot2 = "26263"; //will not throw exception
try {
foo = Integer.parseInt(StringThatCouldBeANumberOrNot);
} catch (NumberFormatException e) {
//Will Throw exception!
//do something! anything to handle the exception.
}
try {
foo = Integer.parseInt(StringThatCouldBeANumberOrNot2);
} catch (NumberFormatException e) {
//No problem this time, but still it is good practice to care about exceptions.
//Never trust user input :)
//Do something! Anything to handle the exception.
}
在尝试从拆分参数中获取整数值或动态解析某些内容时,处理此异常非常重要。
整数代码
您还可以使用公共静态整数解码(Stringnm)抛出NumberFormatException。
它也适用于底座8和16:
// base 10
Integer.parseInt("12"); // 12 - int
Integer.valueOf("12"); // 12 - Integer
Integer.decode("12"); // 12 - Integer
// base 8
// 10 (0,1,...,7,10,11,12)
Integer.parseInt("12", 8); // 10 - int
Integer.valueOf("12", 8); // 10 - Integer
Integer.decode("012"); // 10 - Integer
// base 16
// 18 (0,1,...,F,10,11,12)
Integer.parseInt("12",16); // 18 - int
Integer.valueOf("12",16); // 18 - Integer
Integer.decode("#12"); // 18 - Integer
Integer.decode("0x12"); // 18 - Integer
Integer.decode("0X12"); // 18 - Integer
// base 2
Integer.parseInt("11",2); // 3 - int
Integer.valueOf("11",2); // 3 - Integer
如果要获取int而不是Integer,可以使用:
取消装箱:int val=Integer.decode(“12”);intValue():Integer.decode(“12”).intValue();
实现这一点的两种主要方法是使用Integer类的方法valueOf()和方法parseInt()。
假设给你一个这样的字符串
String numberInString = "999";
然后可以使用
int numberInInteger = Integer.parseInt(numberInString);
或者,您可以使用
int numberInInteger = Integer.valueOf(numberInString);
但这里的问题是,Integer.valueOf()方法在Integer类中有以下实现:
public static Integer valueOf(String var0, int var1) throws NumberFormatException {
return parseInt(var0, var1);
}
如您所见,Integer.valueOf()在内部调用Integer.parseInt()本身。此外,parseInt()返回int,valueOf()则返回Integer
这是一个完整的程序,所有条件都是正的和负的,不使用库
import java.util.Scanner;
public class StringToInt {
public static void main(String args[]) {
String inputString;
Scanner s = new Scanner(System.in);
inputString = s.nextLine();
if (!inputString.matches("([+-]?([0-9]*[.])?[0-9]+)")) {
System.out.println("Not a Number");
}
else {
Double result2 = getNumber(inputString);
System.out.println("result = " + result2);
}
}
public static Double getNumber(String number) {
Double result = 0.0;
Double beforeDecimal = 0.0;
Double afterDecimal = 0.0;
Double afterDecimalCount = 0.0;
int signBit = 1;
boolean flag = false;
int count = number.length();
if (number.charAt(0) == '-') {
signBit = -1;
flag = true;
}
else if (number.charAt(0) == '+') {
flag = true;
}
for (int i = 0; i < count; i++) {
if (flag && i == 0) {
continue;
}
if (afterDecimalCount == 0.0) {
if (number.charAt(i) - '.' == 0) {
afterDecimalCount++;
}
else {
beforeDecimal = beforeDecimal * 10 + (number.charAt(i) - '0');
}
}
else {
afterDecimal = afterDecimal * 10 + number.charAt(i) - ('0');
afterDecimalCount = afterDecimalCount * 10;
}
}
if (afterDecimalCount != 0.0) {
afterDecimal = afterDecimal / afterDecimalCount;
result = beforeDecimal + afterDecimal;
}
else {
result = beforeDecimal;
}
return result * signBit;
}
}
一个方法是parseInt(String)。它返回一个基元int:
String number = "10";
int result = Integer.parseInt(number);
System.out.println(result);
第二个方法是valueOf(String),它返回一个新的Integer()对象:
String number = "10";
Integer result = Integer.valueOf(number);
System.out.println(result);