如何将String转换为int?
"1234" → 1234
如何将String转换为int?
"1234" → 1234
当前回答
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);
}
}
其他回答
我有一个解决方案,但我不知道它有多有效。但它工作得很好,我认为你可以改进它。另一方面,我用JUnit做了几次测试,哪一步正确。我附上了功能和测试:
static public Integer str2Int(String str) {
Integer result = null;
if (null == str || 0 == str.length()) {
return null;
}
try {
result = Integer.parseInt(str);
}
catch (NumberFormatException e) {
String negativeMode = "";
if(str.indexOf('-') != -1)
negativeMode = "-";
str = str.replaceAll("-", "" );
if (str.indexOf('.') != -1) {
str = str.substring(0, str.indexOf('.'));
if (str.length() == 0) {
return (Integer)0;
}
}
String strNum = str.replaceAll("[^\\d]", "" );
if (0 == strNum.length()) {
return null;
}
result = Integer.parseInt(negativeMode + strNum);
}
return result;
}
使用JUnit进行测试:
@Test
public void testStr2Int() {
assertEquals("is numeric", (Integer)(-5), Helper.str2Int("-5"));
assertEquals("is numeric", (Integer)50, Helper.str2Int("50.00"));
assertEquals("is numeric", (Integer)20, Helper.str2Int("$ 20.90"));
assertEquals("is numeric", (Integer)5, Helper.str2Int(" 5.321"));
assertEquals("is numeric", (Integer)1000, Helper.str2Int("1,000.50"));
assertEquals("is numeric", (Integer)0, Helper.str2Int("0.50"));
assertEquals("is numeric", (Integer)0, Helper.str2Int(".50"));
assertEquals("is numeric", (Integer)0, Helper.str2Int("-.10"));
assertEquals("is numeric", (Integer)Integer.MAX_VALUE, Helper.str2Int(""+Integer.MAX_VALUE));
assertEquals("is numeric", (Integer)Integer.MIN_VALUE, Helper.str2Int(""+Integer.MIN_VALUE));
assertEquals("Not
is numeric", null, Helper.str2Int("czv.,xcvsa"));
/**
* Dynamic test
*/
for(Integer num = 0; num < 1000; num++) {
for(int spaces = 1; spaces < 6; spaces++) {
String numStr = String.format("%0"+spaces+"d", num);
Integer numNeg = num * -1;
assertEquals(numStr + ": is numeric", num, Helper.str2Int(numStr));
assertEquals(numNeg + ": is numeric", numNeg, Helper.str2Int("- " + numStr));
}
}
}
使用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);
}
}
// 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);
}
}
对于Java 11,有几种方法可以将int转换为String类型:
1) 整数.parseInt()
String str = "1234";
int result = Integer.parseInt(str);
2) Integer.valueOf()
String str = "1234";
int result = Integer.valueOf(str).intValue();
3) 整数构造函数
String str = "1234";
Integer result = new Integer(str);
4) 整数代码
String str = "1234";
int result = Integer.decode(str);
使用Java integer类的parseInt方法将字符串转换为整数。parseInt方法将字符串转换为int,如果字符串无法转换为int类型,则抛出NumberFormatException。
忽略它可能引发的异常,请使用以下命令:
int i = Integer.parseInt(myString);
如果变量myString表示的字符串是有效的整数,如“1234”、“200”、“1”,它将被转换为Java int。如果由于任何原因失败,则更改可能引发NumberFormatException,因此代码应该稍长一些才能解释这一点。
例如,Java String到int的转换方法,控制可能的NumberFormatException
public class JavaStringToIntExample
{
public static void main (String[] args)
{
// String s = "test"; // Use this if you want to test the exception below
String s = "1234";
try
{
// The String to int conversion happens here
int i = Integer.parseInt(s.trim());
// Print out the value after the conversion
System.out.println("int i = " + i);
}
catch (NumberFormatException nfe)
{
System.out.println("NumberFormatException: " + nfe.getMessage());
}
}
}
如果更改尝试失败(在本例中,如果您可以尝试将Java String测试转换为int),Integer parseInt进程将抛出NumberFormatException,您必须在try/catch块中处理该异常。