如何将String转换为int?
"1234" → 1234
如何将String转换为int?
"1234" → 1234
当前回答
使用Integer.parseInt()并将其放入try。。。catch块来处理任何错误,例如,
private void ConvertToInt(){
String string = txtString.getText();
try{
int integerValue=Integer.parseInt(string);
System.out.println(integerValue);
}
catch(Exception e){
JOptionPane.showMessageDialog(
"Error converting string to integer\n" + e.toString,
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
其他回答
除了前面的答案,我还想添加几个功能。以下是使用时的结果:
public static void main(String[] args) {
System.out.println(parseIntOrDefault("123", 0)); // 123
System.out.println(parseIntOrDefault("aaa", 0)); // 0
System.out.println(parseIntOrDefault("aaa456", 3, 0)); // 456
System.out.println(parseIntOrDefault("aaa789bbb", 3, 6, 0)); // 789
}
实施:
public static int parseIntOrDefault(String value, int defaultValue) {
int result = defaultValue;
try {
result = Integer.parseInt(value);
}
catch (Exception e) {
}
return result;
}
public static int parseIntOrDefault(String value, int beginIndex, int defaultValue) {
int result = defaultValue;
try {
String stringValue = value.substring(beginIndex);
result = Integer.parseInt(stringValue);
}
catch (Exception e) {
}
return result;
}
public static int parseIntOrDefault(String value, int beginIndex, int endIndex, int defaultValue) {
int result = defaultValue;
try {
String stringValue = value.substring(beginIndex, endIndex);
result = Integer.parseInt(stringValue);
}
catch (Exception e) {
}
return result;
}
嗯,需要考虑的一个非常重要的点是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.
}
在尝试从拆分参数中获取整数值或动态解析某些内容时,处理此异常非常重要。
import java.util.*;
public class strToint {
public static void main(String[] args) {
String str = "123";
byte barr[] = str.getBytes();
System.out.println(Arrays.toString(barr));
int result = 0;
for(int i = 0; i < barr.length; i++) {
//System.out.print(barr[i]+" ");
int ii = barr[i];
char a = (char) ii;
int no = Character.getNumericValue(a);
result = result * 10 + no;
System.out.println(result);
}
System.out.println("result:"+result);
}
}
有多种方法可以将字符串int值转换为Integer数据类型值。您需要处理字符串值问题的NumberFormatException。
整数.分析Intfoo=整数.parseInt(myString);整数值foo=Integer.valueOf(myString);使用Java 8可选APIfoo=可选.Nullable(myString).map(整数::parseInt).get();
对于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
}