在Android上将字符串转换为整数

如何将字符串转换为整数?

我有一个文本框,让用户输入一个数字:

EditText et = (EditText) findViewById(R.id.entry1);
String hello = et.getText().toString();

并且该值被赋给字符串hello

我想把它转换成一个整数,这样我就能得到他们输入的数字;稍后将在代码中使用它。

有没有办法让EditText变成一个整数?这样就跳过了中间人。如果不是,字符串到整数就可以了。

648030 次浏览

参见Integer类和静态parseInt()方法:

http://developer.android.com/reference/java/lang/Integer.html

Integer.parseInt(et.getText().toString());

你将需要捕捉NumberFormatException,以防解析时出现问题,因此:

int myNum = 0;


try {
myNum = Integer.parseInt(et.getText().toString());
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
int in = Integer.valueOf(et.getText().toString());
//or
int in2 = new Integer(et.getText().toString());

你应该将String转换为float。它正在起作用。

float result = 0;
if (TextUtils.isEmpty(et.getText().toString()) {
return;
}


result = Float.parseFloat(et.getText().toString());


tv.setText(result);

将字符串转换为int的最佳方法是:

 EditText et = (EditText) findViewById(R.id.entry1);
String hello = et.getText().toString();
int converted=Integer.parseInt(hello);

使用正则表达式:

int i=Integer.parseInt("hello123".replaceAll("[\\D]",""));
int j=Integer.parseInt("123hello".replaceAll("[\\D]",""));
int k=Integer.parseInt("1h2el3lo".replaceAll("[\\D]",""));

输出:

i=123;
j=123;
k=123;

使用正则表达式:

String s="your1string2contain3with4number";
int i=Integer.parseInt(s.replaceAll("[\\D]", ""));
< p >输出: 我= 1234;< / p >

如果你需要第一个数字组合,那么你应该尝试下面的代码:

String s="abc123xyz456";
int i=NumberFormat.getInstance().parse(s).intValue();
< p >输出: 我= 123;< / p >

您可以使用以下方法将字符串解析为整数:

int value = Integer.parseInt (textView.getText () .toString ());

(1) 输入: 12然后它将工作..因为textview已经把这个12数字作为“12”字符串。

(2)输入: "abdul"则它将抛出一个异常,即NumberFormatException。 所以要解决这个问题,我们需要使用try catch,就像我下面提到的:

  int tax_amount=20;
EditText edit=(EditText)findViewById(R.id.editText1);
try
{


int value=Integer.parseInt(edit.getText().toString());
value=value+tax_amount;
edit.setText(String.valueOf(value));// to convert integer to string


}catch(NumberFormatException ee){
Log.e(ee.toString());
}

你也可以参考下面的链接以获得更多信息: http://developer.android.com/reference/java/lang/Integer.html < / >强

你也可以只写一行:

int hello = Integer.parseInt(((Button)findViewById(R.id.button1)).getText().toString().replaceAll("[\\D]", ""));

从执行顺序读取

  1. 使用findViewById(R.id.button1)获取视图
  2. 使用((Button)______)View转换为Button
  3. 调用.GetText()从Button获取文本条目
  4. 调用.toString()将字符变化转换为字符串
  5. "[\\D]"调用.ReplaceAll(),将所有非数字字符替换为""(无)
  6. 调用Integer.parseInt()抓取并从数字字符串中返回一个整数。

试试这段代码,它真的工作。

int number = 0;
try {
number = Integer.parseInt(YourEditTextName.getText().toString());
} catch(NumberFormatException e) {
System.out.println("parse value is not valid : " + e);
}

使用正则表达式是做到这一点的最好方法,正如ashish sahu已经提到的那样

public int getInt(String s){
return Integer.parseInt(s.replaceAll("[\\D]", ""));
}

更简单的方法是使用Integerdecode方法,例如:

int helloInt = Integer.decode(hello);

芬兰湾的科特林

可以使用Extension方法将它们解析为其他基本类型。

Java

String num = "10";
Integer.parseInt(num );
有五种转换方法 第一种方法:

String str = " 123" ;
int i = Integer.parse(str);
output : 123

第二种方式:

String str = "hello123world";
int i = Integer.parse(str.replaceAll("[\\D]" , "" ) );
output : 123

第三种方式:

String str"123";
int i = new Integer(str);
output "123

第四种方式:

String str"123";
int i = Integer.valueOf(Str);
output "123

第五种方式:

String str"123";
int i = Integer.decode(str);
output "123

可能有其他方法 但这就是我现在所记得的

我只是改变了这个

int j = Integer.parseInt(user.replaceAll("[\\D]", ""));

int j = 1;

它解决了我的问题