如何将String转换为int?
"1234" → 1234
如何将String转换为int?
"1234" → 1234
当前回答
int foo = Integer.parseInt("1234");
确保字符串中没有非数字数据。
其他回答
// As per your question "1234" → 1234
//using Integer.parseInt() method
public class StringToIntExample1{
public static void main(String args[]){
//Declaring String variable
String s="1234";
//Converting String into int using Integer.parseInt()
int i=Integer.parseInt(s);
//Printing value of i
System.out.println(i);
}
}
嗯,需要考虑的一个非常重要的点是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.
}
在尝试从拆分参数中获取整数值或动态解析某些内容时,处理此异常非常重要。
例如,这里有两种方法:
Integer x = Integer.valueOf(str);
// or
int y = Integer.parseInt(str);
这些方法略有不同:
valueOf返回java.lang.Integer的新实例或缓存实例parseInt返回基元int。
所有情况都是一样的:Short.valueOf/parseShort、Long.valueOf/parseLong等。
自定义算法:
public static int toInt(String value) {
int output = 0;
boolean isFirstCharacter = true;
boolean isNegativeNumber = false;
byte bytes[] = value.getBytes();
for (int i = 0; i < bytes.length; i++) {
char c = (char) bytes[i];
if (!Character.isDigit(c)) {
isNegativeNumber = (c == '-');
if (!(isFirstCharacter && (isNegativeNumber || c == '+'))) {
throw new NumberFormatException("For input string \"" + value + "\"");
}
} else {
int number = Character.getNumericValue(c);
output = output * 10 + number;
}
isFirstCharacter = false;
}
if (isNegativeNumber)
output *= -1;
return output;
}
另一种解决方案:
(使用string charAt方法,而不是将字符串转换为字节数组)
public static int toInt(String value) {
int output = 0;
boolean isFirstCharacter = true;
boolean isNegativeNumber = false;
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
if (!Character.isDigit(c)) {
isNegativeNumber = (c == '-');
if (!(isFirstCharacter && (isNegativeNumber || c == '+'))) {
throw new NumberFormatException("For input string \"" + value + "\"");
}
} else {
int number = Character.getNumericValue(c);
output = output * 10 + number;
}
isFirstCharacter = false;
}
if (isNegativeNumber)
output *= -1;
return output;
}
示例:
int number1 = toInt("20");
int number2 = toInt("-20");
int number3 = toInt("+20");
System.out.println("Numbers = " + number1 + ", " + number2 + ", " + number3);
try {
toInt("20 Hadi");
} catch (NumberFormatException e) {
System.out.println("Error: " + e.getMessage());
}
您只需尝试以下操作:
使用Integer.parseInt(your_string);将字符串转换为int使用Double.parseDouble(your_string);将字符串转换为double
实例
String str = "8955";
int q = Integer.parseInt(str);
System.out.println("Output>>> " + q); // Output: 8955
String str = "89.55";
double q = Double.parseDouble(str);
System.out.println("Output>>> " + q); // Output: 89.55