如何将String转换为int?
"1234" → 1234
如何将String转换为int?
"1234" → 1234
当前回答
嗯,需要考虑的一个非常重要的点是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.
}
在尝试从拆分参数中获取整数值或动态解析某些内容时,处理此异常非常重要。
其他回答
将字符串转换为int比仅转换数字更复杂。您已经考虑了以下问题:
字符串是否只包含数字0-9?字符串之前或之后的-/+怎么了?这是可能的吗(指会计数字)?MAX_-/MIN_INFINITY怎么了?如果字符串为99999999999999999999,会发生什么?机器可以将此字符串视为int吗?
您可以有自己的实现,例如:
public class NumericStringToInt {
public static void main(String[] args) {
String str = "123459";
int num = stringToNumber(str);
System.out.println("Number of " + str + " is: " + num);
}
private static int stringToNumber(String str) {
int num = 0;
int i = 0;
while (i < str.length()) {
char ch = str.charAt(i);
if (ch < 48 || ch > 57)
throw new NumberFormatException("" + ch);
num = num * 10 + Character.getNumericValue(ch);
i++;
}
return num;
}
}
除了所有这些答案,我发现了一种新的方法,尽管它在内部使用Integer.parseInt()。
通过使用
import javafx.util.converter.IntegerStringConverter;
new IntegerStringConverter().fromString("1234").intValue()
or
new IntegerStringConverter().fromString("1234")
尽管随着新对象的创建,它的成本有点高。
只需浏览javafx.util.StringConverter<T>类。它有助于将任何包装器类值转换为字符串,反之亦然。
实现这一点的两种主要方法是使用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
目前我正在做一项大学作业,在那里我不能使用某些表达式,例如上面的表达式,通过查看ASCII表,我成功地做到了这一点。这是一个复杂得多的代码,但它可以帮助像我一样受到限制的其他人。
首先要做的是接收输入,在本例中是一串数字;我将其称为String number,在本例中,我将使用数字12来举例说明,因此String number=“12”;
另一个限制是我不能使用重复的循环,因此,也不能使用for循环(这是完美的)。这限制了我们一点,但这也是我们的目标。由于我只需要两个数字(取最后两个数字),一个简单的charAt解决了这个问题:
// Obtaining the integer values of the char 1 and 2 in ASCII
int semilastdigitASCII = number.charAt(number.length() - 2);
int lastdigitASCII = number.charAt(number.length() - 1);
有了代码,我们只需要查看表格,并进行必要的调整:
double semilastdigit = semilastdigitASCII - 48; // A quick look, and -48 is the key
double lastdigit = lastdigitASCII - 48;
现在,为什么要加倍?嗯,因为一个非常“奇怪”的步骤。目前我们有两个双打,1和2,但我们需要将其转换为12,我们无法进行任何数学运算。
我们将后者(最后一个数字)除以10,以2/10=0.2的方式(因此为什么要加倍),如下所示:
lastdigit = lastdigit / 10;
这只是在玩弄数字。我们正在把最后一位数字变成小数。但现在,看看会发生什么:
double jointdigits = semilastdigit + lastdigit; // 1.0 + 0.2 = 1.2
没有太多的数学知识,我们只是简单地将数字的数字分离出来。你看,因为我们只考虑0-9,所以除以10的倍数就像创建一个“盒子”来存储它(回想一下一年级老师向你解释什么是单位和一百)。因此:
int finalnumber = (int) (jointdigits*10); // Be sure to use parentheses "()"
就这样。考虑到以下限制,您将数字字符串(在本例中为两个数字)转换为由这两个数字组成的整数:
无重复循环没有parseInt等“魔法”表达式