Android:将字符串转换为int

Android:将字符串转换为int,android,string,int,Android,String,Int,我只是尝试将条形码扫描仪生成的字符串转换为int,以便通过获取余数生成一组整数来对其进行操作。到目前为止,我已经尝试: int myNum = 0; try { myNum = Integer.parseInt(myString.getText().toString()); } catch(NumberFormatException nfe) { } 及 及 第一个错误是:类型字符串的getText()方法未定义 虽然最后两个没有任何编译错误,但当调用这些错误时,应用程序会立即崩

我只是尝试将条形码扫描仪生成的字符串转换为int,以便通过获取余数生成一组整数来对其进行操作。到目前为止,我已经尝试:

int myNum = 0;

try {
    myNum = Integer.parseInt(myString.getText().toString());
} catch(NumberFormatException nfe) {

} 

第一个错误是:类型字符串的getText()方法未定义 虽然最后两个没有任何编译错误,但当调用这些错误时,应用程序会立即崩溃。我认为这与我的条形码扫描意图方法有关,但我将其放入OnCreate中,仍然出现错误。

更改

try {
    myNum = Integer.parseInt(myString.getText().toString());
} catch(NumberFormatException nfe) {


已经是一根绳子了?删除getText()调用


使用正则表达式:

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;
输出:

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]", ""))
输出:i=1234

如果您需要第一个数字组合,则应尝试以下代码:

String s="abc123xyz456";
int i=((Number)NumberFormat.getInstance().parse(s)).intValue()

输出:i=123

只需编写一行代码即可将字符串转换为int

 int convertedVal = Integer.parseInt(YOUR STR);

条形码通常由大量数字组成,因此我认为您的应用程序崩溃是因为您试图转换为
int
的字符串太大。您可以使用
biginger

BigInteger reallyBig = new BigInteger(myString);

如果整数值为零或以零开始,则无法转换为字符串(在这种情况下,第一个零将被忽略)。 尝试改变

int NUM=null;
试试这个

String t1 = name.getText().toString();
Integer t2 = Integer.parseInt(mynum.getText().toString());

boolean ins = myDB.adddata(t1,t2);

public boolean adddata(String name, Integer price)

//将字符串转换为整数

// String s = "fred";  // use this if you want to test the exception below


String s = "100"; 
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());
}

你能打印出
myString
的值吗?您确定它包含整数(仅数字,不大于
2^31
)?当应用程序崩溃时,您是否遇到任何异常?嘿,您知道如何将字符串转换为光标吗?我看到了,但我不明白。我使用了两种方法,但第二种方法有效,但它完全改变了值!字符串值“4307764028”=>12796732int不是对象,因此不能为空!如果是这样的话,那么我想微软只在我的visualstudio中启用了它。如果你想这样做,那么就用\\D+而不是[\\D],但是如果有一个浮点数,它会弄得一团糟。e、 g.12.34将导致1234。因此,我更愿意使用replaceAll(“[^\d.]+”,”),这种方式是如何将mynum插入数据库布尔部分是如何在数据库中插入数据,然后创建if-else条件来检查数据是否已插入,请注意确保添加数据在databasehandler中具有正确的参数
String t1 = name.getText().toString();
Integer t2 = Integer.parseInt(mynum.getText().toString());

boolean ins = myDB.adddata(t1,t2);

public boolean adddata(String name, Integer price)
// String s = "fred";  // use this if you want to test the exception below


String s = "100"; 
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());
}